사용자 도구

사이트 도구


springframework:propertysource

문서의 이전 판입니다!


PropertySource

  • Spring 3.1 부터 생기기 시작한 통합 프라퍼티 관리 시스템
  • 시스템 프라퍼티, 환경변수, JNDI 등을 모두 하나의 공간에 넣고 그 값을 읽고 설정할 수 있게 해준다.

프라퍼티 추가

코딩을 통한 임의의 프라퍼티 소스 추가

  • MyPropertySourcePropertySource 구현체 일 때
  • 일반 Java 애플리케이션
    ConfigurableApplicationContext ctx = new GenericApplicationContext();
    MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
    sources.addFirst(new MyPropertySource());
  • 웹 애플리케이션
    • web.xml
      <context-param>
          <param-name>contextInitializerClasses</param-name>
          <param-value>com.bank.MyInitializer</param-value>
      </context-param>
    • MyInitializer.java
      public class MyInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
          public void initialize(ConfigurableWebApplicationContext ctx) {
              PropertySource ps = new MyPropertySource();
              ctx.getEnvironment().getPropertySources().addFirst(ps);
              // perform any other initialization of the context ...
          }
      }

PlaceHolder로 사용하기

PropertySource에 등록된 프라퍼티들을 @Value(“${property.name}”} 형태로 사용하려면 PropertySourcesPlaceholderConfigurer 등록이 필요하다.

  • Java 코드 설정시
    @Configuration
    @PropertySource("my.properties")
    public class SpringConfig {
      @Bean
      public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
      }
    }
  • XML로 설정시
      <context:property-placeholder location="classpath:my-properties.xml"/>
      <!--
          <context:property-placeholder/>는 Spring 3.1에서는 PropertySourcePlaceholderConfigurer 로 초기화된다.
          이 때 location에 지정된 프라퍼티도 place-holder로 적용하게 된다.
          현재(Spring 3.1.2)에는 XML Property를 못 읽는 버그가 있는데 이를 통해 우회 가능하다.
          단, 이렇게 location에 들어간 프라퍼티는 공식 Environment의 PropertySource로 추가되지는 않는다.
          따라서 org.springframework.core.env.Environment.getProperty()로는 읽을 수 없다.
          @ 확인 필요 : 이 경우 자식 ApplicationContext에서도 location에 지정된 프라퍼티는 place-holder로 사용할 수 없을 것이다. @
      -->
springframework/propertysource.1350734670.txt.gz · 마지막으로 수정됨: 2012/10/20 21:04 저자 kwon37xi