Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

spring property substitution for test and production

I ran into this for property substitution in spring

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

but unfortunately, we don't want this in the xml file as we want to re-use the file in our tests but swap in the test.properties file for test. ie. we want to test all the production bindings but with properties that are suitable for test like localhost for instance. How can we load the ApplicationContext but with different properties files?

thanks, Dean

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

several approaches:


1. 'Order' Property

in src/main/resources/your-conf.xml

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

in src/test/resources/your-test-config.xml

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

If you running your test with src/test/resources as a test classpath, the above will ensure to override src/main/resources/esb-project-config.properties with the src/test/resources/esb-project-config.properties.

This will override the whole property-placeholder though, so you would have to provide all the properties needed in your application in for this test property-placeholder. e.g.

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

2. PropertyOverrideConfigurer

 <context:property-override 
          location="classpath:esb-project-config.test.properties"/>

to override certain individual properties. Some examples here


3. System Variables

You can use a prefix to control environment specific properties, this can be done by using system variables:

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

In this case it will always look under:

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

by default, unless a ENV_SYSTEM system variable is set. If it is set to qa, for example, it will automatically look under:

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

4. Spring Profiles

Another approach is to make beans profile specific. For example:

<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>

The appropriate esb-project-config will loaded depending on a profile set. For example this will load 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();

  • NOTE: "System Variables" and "System Profiles" approaches are usually used to switch between different environments rather than just "dev <==> test" in dev mode, but still are useful capabilities to be aware of.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...