문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
springframework:propertysource [2012/10/20 21:19] kwon37xi [코딩을 통한 임의의 프라퍼티 소스 추가] |
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) | ||
</ | </ | ||
* '' | * '' | ||
줄 35: | 줄 39: | ||
</ | </ | ||
- | ==== @Configuration 클래스에서 | + | ==== ApplicationContextInitializer를 Java 코드 기반 ApplicationContext에서 |
- | 현재(3.1.2) '' | + | * [[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(); | ||
- | <code java> | + | // ApplicationContextInitializer가 ApplicationContext를 초기화해주는 형태 |
+ | initializer.initialize( ctx ); | ||
+ | // @Configuration 클래스를 등록하고 초기화 | ||
+ | ctx.register( com.my.classpath.StackOverflowConfiguration.class ); | ||
+ | ctx.refresh() | ||
+ | </ | ||
+ | |||
+ | ==== @Configuration 클래스에서 XML 프라퍼티를 프라퍼티 소스로 등록하기 예제 ==== | ||
+ | **3.2 최신 버전에서는 이 기법을 사용할 필요가 없다. ApplicationContextInitializer를 사용하자.** | ||
+ | 현재(3.1.2) '' | ||
+ | [[http:// | ||
+ | |||
+ | <code java> | ||
@Configuration | @Configuration | ||
- | @ComponentScan(basePackages = " | ||
- | @PropertySource(" | ||
public class SpringConfig { | public class SpringConfig { | ||
@Autowired | @Autowired | ||
- | private ConfigurableApplicationContext applicationContext; | + | private |
+ | |||
+ | @Autowired | ||
+ | private org.springframework.core.io.ResourceLoader resourceLoader; | ||
@PostConstruct | @PostConstruct | ||
public void addXmlProperties() throws Exception { | public void addXmlProperties() throws Exception { | ||
+ | addXmlProperties(" | ||
+ | addXmlProperties(" | ||
+ | } | ||
+ | |||
+ | private void addXmlProperties(String location) throws IOException, | ||
+ | InvalidPropertiesFormatException { | ||
+ | Resource resource = resourceLoader.getResource(location); | ||
+ | |||
Properties properties = new Properties(); | Properties properties = new Properties(); | ||
- | properties.loadFromXML(new FileInputStream(new File( | + | properties.loadFromXML(resource.getInputStream()); |
- | " | + | |
applicationContext | applicationContext | ||
.getEnvironment() | .getEnvironment() | ||
.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; | ||
} | } | ||
- | // PropertiesSourcesPlaceholderConfigurer도 잘 작동한다. | ||
@Bean | @Bean | ||
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { | public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { | ||
줄 70: | 줄 107: | ||
===== PlaceHolder로 사용하기 ===== | ===== PlaceHolder로 사용하기 ===== | ||
- | PropertySource에 등록된 프라퍼티들을 '' | + | PropertySource에 등록된 프라퍼티들을 '' |
* Java 코드 설정시< | * Java 코드 설정시< | ||
@Configuration | @Configuration | ||
줄 86: | 줄 123: | ||
< | < | ||
이 때 location에 지정된 프라퍼티도 place-holder로 적용하게 된다. | 이 때 location에 지정된 프라퍼티도 place-holder로 적용하게 된다. | ||
- | 현재(Spring 3.1.2)에는 XML Property를 못 읽는 버그가 있는데 이를 통해 우회 가능하다. | + | 현재(Spring 3.1.2)에는 XML Property를 못 추가하는 버그가 있는데 이를 통해 우회 가능하다. |
단, 이렇게 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:// |