복붙노트

[SPRING] 시험 및 생산을위한 스프링 특성 대체

SPRING

시험 및 생산을위한 스프링 특성 대체

나는 봄에 재산 대체를 위해 이것을 만났다.

<context:property-placeholder location="esb-project-config.properties"/>

그러나 불행하게도, 우리는 우리의 테스트에서 파일을 재사용하기를 원하지만 테스트를 위해 test.properties 파일을 바꿔 가면서 xml 파일에 이것을 원하지 않습니다. 즉. 우리는 모든 프로덕션 바인딩을 테스트하려고하지만 예를 들어 localhost와 같은 테스트에 적합한 속성을 사용하려고합니다. ApplicationContext를 다른 등록 정보 파일로 어떻게로드 할 수 있습니까?

감사, 학장

해결법

  1. ==============================

    1.추가 스프링 XML 구성 파일에 속성 자리 표시 자 구성을 넣습니다.

    추가 스프링 XML 구성 파일에 속성 자리 표시 자 구성을 넣습니다.

    예 :

    웹 응용 프로그램에서는이 패턴 applicationContext * .xml을 사용하여 모든 제작용 스프링 컨텍스트 파일을로드 할 수 있습니다.

    테스트에는 testApplicationContext.xml 만로드하면됩니다. 여기에는 일반 구성이 포함되지만 다른 특성이 포함됩니다.

  2. ==============================

    2.몇 가지 접근법 :

    몇 가지 접근법 :

    src / main / resources / your-conf.xml에 있습니다.

    <context:property-placeholder 
             location="classpath:esb-project-config.properties"
             order="1"/>
    

    src / test / resources / your-test-config.xml에 있습니다.

    <context:property-placeholder 
             location="classpath:esb-project-config.properties"
             order="0"/>
    

    src / test / resources를 테스트 클래스 패스로 사용하여 테스트를 실행하면 위의 명령을 실행하면 src / main / resources / esb-project-config.properties를 src / test / resources / esb-project-config.properties로 대체 할 수 있습니다. .

    이렇게하면 전체 속성 자리 표시자를 무시하므로이 테스트 속성 자리 표시 자에 대해 응용 프로그램에 필요한 모든 속성을 제공해야합니다. 예 :

    <context:property-placeholder 
             location="classpath:esb-project-config.properties,
                       classpath:some-other-props-if-needed.properties"
             order="0"/>
    
     <context:property-override 
              location="classpath:esb-project-config.test.properties"/>
    

    특정 개별 속성을 무시할 수 있습니다. 여기에 몇 가지 예가있다.

    접두어를 사용하여 환경 별 속성을 제어 할 수 있습니다.이 작업은 시스템 변수를 사용하여 수행 할 수 있습니다.

     <context:property-placeholder 
              location="${ENV_SYSTEM:dev}/esb-project-config.properties"/>
    

    이 경우 항상 아래에 표시됩니다.

     <context:property-placeholder 
              location="dev/esb-project-config.properties"/>
    

    ENV_SYSTEM 시스템 변수가 설정되어 있지 않으면 기본적으로. 예를 들어 qa로 설정하면 자동으로 아래에 표시됩니다.

     <context:property-placeholder 
              location="qa/esb-project-config.properties"/>
    

    또 다른 방법은 bean을 특정 프로파일로 만드는 것입니다. 예 :

    <beans profile="dev">
      <context:property-placeholder 
               location="esb-project-config.dev.properties"/>
    </beans>
    
    <beans profile="qa">
      <context:property-placeholder 
               location="esb-project-config.qa.properties"/>
    </beans>
    

    적절한 esb-project-config는 프로필 집합에 따라로드됩니다. 예를 들어 esb-project-config.dev.properties를로드합니다.

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.getEnvironment().setActiveProfiles( "dev" );
    ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
    ctx.refresh();
    
  3. ==============================

    3.우리는 다음 세 가지 기능을 사용합니다.

    우리는 다음 세 가지 기능을 사용합니다.

    두 개의 등록 정보 파일을 선언합니다.

    classpath:esb-project-config.properties,
    classpath:esb-project-config-override.properties
    

    첫 번째 특성 파일에는 적절한 기본값과 개발 구성이 들어 있습니다. 이 파일은 응용 프로그램의 일부입니다.

    두 번째 특성 파일은 테스트 클래스 경로 또는 응용 프로그램 서버의 프로덕션 클래스 경로에서 사용 가능한 파일입니다. 이 파일은 응용 프로그램 외부에 있습니다. 그렇게하면 각 환경의 속성을 무시하고 응용 프로그램의 버전을 하나만 사용할 수 있습니다.

    여기에 우리가 사용하는 속성의 예가 있습니다.

        <context:property-placeholder 
           ignore-resource-not-found="true" ignore-unresolvable="true" 
           location="classpath:esb-project-config.properties,classpath:esb-project-config-override.properties" />
    
  4. ==============================

    4.봄 3.1에 추가 된 나의 선호하는 방법은 다음과 같다 :

    봄 3.1에 추가 된 나의 선호하는 방법은 다음과 같다 :

    당신의 * -context.xml :

    <context:property-placeholder location="classpath:/web-${spring.profiles.active}.properties" />
    

    및 web.xml :

    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>prod</param-value>
    </context-param>
    

    그런 다음 런타임시 환경을 지정할 수 있습니다 (예 :

    mvn -Dspring.profiles.active=dev jetty:run
    

    그러나 컨테이너에 인수를 전달합니다.

  5. ==============================

    5.그것은 보인다 :

    그것은 보인다 :

      <beans profile="dev">
        <context:property-placeholder location="classpath:config/dev.properties"/>
      </beans>
      <beans profile="prod">
        <context:property-placeholder location="classpath:config/prod.properties"/>
      </beans>
    

    작동하지 않습니다. 그러나 당신은 할 수있다 :

    <context:property-placeholder location="classpath:config_${spring.profiles.active}.properties" />
    
  6. from https://stackoverflow.com/questions/9470551/spring-property-substitution-for-test-and-production by cc-by-sa and MIT license