loading properties file in spring

后端 未结 3 818
星月不相逢
星月不相逢 2021-01-14 01:28

One of our team has implemented loading properties this way (see pseudo code below) and advises this approach is right as the client application using this is free to keep t

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 02:29

    You can put the properties in any file and still use PropertyPlaceholderConfigurer. Here's an example that satisfies both your coworker's concerns and your desire for environment specific stuff:

    
        
            
                
                classpath:MyCompany.properties
                
                classpath:MyCompany.${mycompany.env:dev}.properties
                
                classpath:${mycoworker}
                
                file:///${user.home}/MyCompany.properties
            
        
        
        
        
         
    
    

    If you pass in no -D arguments, then you'll pick up the following properties files, where properties in the later files overwrite previously determined values.

    1. MyCompany.properties off the classpath
    2. MyCompany.dev.properties off the classpath
    3. $HOME/MyCompany.properties if it exists

    To swap in a production config for #2, just pass -Dmycompany.env=prod to java. Similarly your coworker can pass -Dmycoworker=/some/path/config.properties if he/she wants.

提交回复
热议问题