| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 이펙티브
- node
- 후기
- jface
- effective
- elasticsearch
- 엘라스틱서치
- javascript
- nodejs
- RCP
- java
- 인터페이스
- error
- 자바
- Spring Boot
- 독후감
- 자바스크립트
- MySQL
- kibana
- 맛집
- 리뷰
- Git
- 백준
- boot
- Web
- 스프링
- JPA
- java8
- 알고리즘
- Spring
Archives
- Today
- Total
wedul
외부 properties 파일을 이용해서 스프링 빈을 생성하는 방법 본문
반응형
1. XML에서 프로퍼티 설정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // xml <context:property-placeholder location="classpath:database.properties" /> <bean id="dataSource" class="............."> <property name="driverClass" value="${db.driver}" /> <property name="jdbcUrl" value="${db.jdbcUrl}" /> <property name="user" value="${db.user}" /> <property name="password" value="${db.password}" /> </bean> // properties 파일 db.driver=com.mysql.jdbc.Driver db.jdbcUrl=jdbc:mysql://localhost/spring4fs?characterEncoding=utf8 db.user=test db.password=test | cs |
<context:property-placeholder> 태그는 location 속성으로 지정한 프로퍼티 파일로부터 정보를 읽어와 빈 설정에 입력한 플레이스 홀더의 값을 프로퍼티 파일에 존재하는 값으로 변경한다.
# place holder는 ${로 시작하고 }로 끝나는 값.
주의 할 점은
서로 다른 xml에서 서로다른 위치에 프로퍼티 파일을 사용한다고 해도
먼저 열린 프로퍼티 값이 우선순위를 가지게 되어 두 번째 프로퍼티 파일이 열리지 않을 수 있다.
예를 들면
1 2 3 4 5 | test.xml에서 <context:property-placeholder location="classpath:test.properties" /> db.xml에서 <context:property-placeholder location="classpath:db.properties" /> | cs |
오류 메시지
Could not resolve placeholder .... in string value ...
이를 해결하기 위해서는 한 곳에서 placeholder를 사용하도록 구성하는 것이 좋다.
2. @Configuartion 애노테이션 이용 자바 설정에서의 프로퍼티 사용
@Configuration
public class Config {
@Value("${db.driver}")
private String driver;
@Bean
public static PropertySourcesPlaceHolderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResources("test.properties"));
return configurer;
}
반응형
'web > Spring' 카테고리의 다른 글
| Mybatis에서 parameterType을 List로 insert하기 (0) | 2018.05.27 |
|---|---|
| Spring 프로젝트에서 src/main/resource 경로에 파일접근 방법 (0) | 2018.05.27 |
| java.lang.Error: Unresolved compilation problem (0) | 2018.05.27 |
| Mybatis String을 ParameterType으로 넘길때 (0) | 2018.05.27 |
| spring에서 List 또는 Array 데이터를 Controller에서 받기 (0) | 2018.05.27 |
