spring property substitution for test and production

前端 未结 5 1146
余生分开走
余生分开走 2020-11-30 19:40

I ran into this for property substitution in spring


but u

5条回答
  •  渐次进展
    2020-11-30 20:14

    several approaches:


    1. 'Order' Property

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

    
    

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

    
    

    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.

    
    

    2. PropertyOverrideConfigurer

     
    

    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:

     
    

    In this case it will always look under:

     
    

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

     
    

    4. Spring Profiles

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

    
      
    
    
    
      
    
    

    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.

提交回复
热议问题