문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
springframework:propertysource [2012/10/20 21:30] kwon37xi [PlaceHolder로 사용하기] |
springframework:propertysource [2017/08/11 09:04] (현재) kwon37xi |
||
---|---|---|---|
줄 3: | 줄 3: | ||
* Spring 3.1 부터 생기기 시작한 통합 프라퍼티 관리 시스템 | * Spring 3.1 부터 생기기 시작한 통합 프라퍼티 관리 시스템 | ||
* 시스템 프라퍼티, | * 시스템 프라퍼티, | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
===== 프라퍼티 추가 ===== | ===== 프라퍼티 추가 ===== | ||
* 일반 프라퍼티 파일의 경우 | * 일반 프라퍼티 파일의 경우 | ||
* Java 클래스 설정에서는 [[http:// | * Java 클래스 설정에서는 [[http:// | ||
- | * [[http:// | + | * <del>[[http:// |
===== 코딩을 통한 임의의 프라퍼티 소스 추가 ===== | ===== 코딩을 통한 임의의 프라퍼티 소스 추가 ===== | ||
줄 24: | 줄 25: | ||
< | < | ||
</ | </ | ||
+ | </ | ||
+ | * TestContext에서 '' | ||
+ | @ContextConfiguration(initializers = MyInitializer.class) | ||
</ | </ | ||
* '' | * '' | ||
줄 33: | 줄 37: | ||
} | } | ||
} | } | ||
+ | </ | ||
+ | |||
+ | ==== ApplicationContextInitializer를 Java 코드 기반 ApplicationContext에서 사용하기 ==== | ||
+ | * [[http:// | ||
+ | // Create context, but dont initialize with configuration by calling | ||
+ | // the empty constructor. Instead, initialize it with the Context Initializer. | ||
+ | AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); | ||
+ | MyAppContextInitializer initializer = new MyAppContextInitializer(); | ||
+ | |||
+ | // ApplicationContextInitializer가 ApplicationContext를 초기화해주는 형태 | ||
+ | initializer.initialize( ctx ); | ||
+ | |||
+ | // @Configuration 클래스를 등록하고 초기화 | ||
+ | ctx.register( com.my.classpath.StackOverflowConfiguration.class ); | ||
+ | ctx.refresh() | ||
</ | </ | ||
==== @Configuration 클래스에서 XML 프라퍼티를 프라퍼티 소스로 등록하기 예제 ==== | ==== @Configuration 클래스에서 XML 프라퍼티를 프라퍼티 소스로 등록하기 예제 ==== | ||
+ | **3.2 최신 버전에서는 이 기법을 사용할 필요가 없다. ApplicationContextInitializer를 사용하자.** | ||
현재(3.1.2) '' | 현재(3.1.2) '' | ||
- | [[http:// | + | [[http:// |
<code java> | <code java> | ||
줄 44: | 줄 64: | ||
@Autowired | @Autowired | ||
- | private ConfigurableApplicationContext applicationContext; | + | private |
@Autowired | @Autowired | ||
- | private org.springframework.core.io.ResourceLoader resourceLoader; | + | private org.springframework.core.io.ResourceLoader resourceLoader; |
@PostConstruct | @PostConstruct | ||
public void addXmlProperties() throws Exception { | public void addXmlProperties() throws Exception { | ||
addXmlProperties(" | addXmlProperties(" | ||
+ | addXmlProperties(" | ||
} | } | ||
private void addXmlProperties(String location) throws IOException, | private void addXmlProperties(String location) throws IOException, | ||
InvalidPropertiesFormatException { | InvalidPropertiesFormatException { | ||
- | | + | Resource resource = resourceLoader.getResource(location); |
Properties properties = new Properties(); | Properties properties = new Properties(); | ||
줄 64: | 줄 85: | ||
.getPropertySources() | .getPropertySources() | ||
.addLast( | .addLast( | ||
- | new PropertiesPropertySource(" | + | new PropertiesPropertySource( |
- | properties)); | + | |
+ | } | ||
+ | |||
+ | // from ResourcePropertySource | ||
+ | private static String getNameForResource(Resource resource) { | ||
+ | String name = resource.getDescription(); | ||
+ | if (!StringUtils.hasText(name)) { | ||
+ | name = resource.getClass().getSimpleName() + " | ||
+ | + System.identityHashCode(resource); | ||
+ | } | ||
+ | return name; | ||
} | } | ||
줄 73: | 줄 104: | ||
} | } | ||
} | } | ||
- | |||
- | |||
</ | </ | ||
===== PlaceHolder로 사용하기 ===== | ===== PlaceHolder로 사용하기 ===== | ||
- | PropertySource에 등록된 프라퍼티들을 '' | + | PropertySource에 등록된 프라퍼티들을 '' |
* Java 코드 설정시< | * Java 코드 설정시< | ||
@Configuration | @Configuration | ||
줄 97: | 줄 126: | ||
단, 이렇게 location에 들어간 프라퍼티는 공식 Environment의 PropertySource로 추가되지는 않는다. | 단, 이렇게 location에 들어간 프라퍼티는 공식 Environment의 PropertySource로 추가되지는 않는다. | ||
따라서 org.springframework.core.env.Environment.getProperty()로는 읽을 수 없다. | 따라서 org.springframework.core.env.Environment.getProperty()로는 읽을 수 없다. | ||
+ | 가능하면 위에 설명한 @Configuration Java 클래스에서 프라퍼티 소스 등록하기 예제를 사용하는게 낫다. | ||
@ 확인 필요 : 이 경우 자식 ApplicationContext에서도 location에 지정된 프라퍼티는 place-holder로 사용할 수 없을 것이다. @ | @ 확인 필요 : 이 경우 자식 ApplicationContext에서도 location에 지정된 프라퍼티는 place-holder로 사용할 수 없을 것이다. @ | ||
--> | --> | ||
</ | </ | ||
+ | |||
+ | ===== ResourceLoader ===== | ||
+ | * '' | ||
+ | * 단, '' | ||
+ | * 그냥 속 편하게 [[http:// | ||
+ | |||
+ | ===== @Value ===== | ||
+ | ==== @Value로 Date 객체 주입하기 ==== | ||
+ | * [[http:// | ||
+ | @Value("# | ||
+ | private Date date; | ||
+ | </ | ||
+ | * [[http:// |