Spring boot properties file

One of our teams implemented the loading properties this way (see the pseudo-code below) and recommends this approach correctly, since the client application using this freely saves the properties in any file. Unlike the widely used property propertyholderfigurer.

context.xml applications

<bean class="com.mypackage.Myclass"> <property name="xml" value="classpath:"{com.myapp.myproperty1}"> </property> </bean> 

config.properties

 com.myapp.myproperty1=data.xml 

edit: I would add that it is data.properties, not data.xml. We want to load the properties file (this properties file is specified in config.properties as "property". Com.myapp.myproperty1 = data.properties

java class

 import org.springframework.core.io.Resource; public class Myclass { private Resource xmlField; // setter & getter methods.. } 

Is it correct to use spring core.io.Resource?

Another reason is that the client application wants to load an environment-specific configuration. I suggested using theconfigurer property and using maven profiles to generate an environment-specific build

Could you advise which one is suitable? and if it differs in different scenarios, please help me point them out?

thanks

+4
source share
3 answers

You can put properties in any file and use PropertyPlaceholderConfigurer . Here is an example that satisfies the concerns of your colleagues and your desire for a specific environment:

 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- default settings --> <value>classpath:MyCompany.properties</value> <!-- environment-specific settings --> <value>classpath:MyCompany.${mycompany.env:dev}.properties</value> <!-- keep your coworker happy --> <value>classpath:${mycoworker}</value> <!-- allows emergency reconfiguration via the local file system --> <value>file:///${user.home}/MyCompany.properties</value> </list> </property> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="ignoreResourceNotFound" value="true" /> <!-- should be validated separately, in case users of the library load additional properties --> <property name="ignoreUnresolvablePlaceholders" value="false"/> </bean> 

If you pass the -D no arguments, you will get the following property files in which the properties in the later files overwrite previously defined values.

  • MyCompany.properties from classpath
  • MyCompany.dev.properties from classpath
  • $ HOME / MyCompany.properties if it exists

To swap the production configuration to # 2, just go -Dmycompany.env=prod in java. Likewise, your colleague can pass -Dmycoworker=/some/path/config.properties if he wants to.

+10
source

I'm not sure why PropertyPlaceholderConfigurator would not be the right choice.

I almost always handled the environment-specific configuration with a custom PPC that could (a) get the -D option at startup and / or (b) use the machine name to decide which property file to load.

For me, this is more convenient than linking information through Maven, since I can more easily test arbitrary configurations from any machine I work on (using the -D property).

+5
source

+1 for Dave's suggestion. You should use PropertyPlaceholderConfigurer to load / read properties. Here is an example that I just pulled from my previous project if you are wondering how to use it. This example is intended to load multiple property files, but the concept is the same. Good luck.

 <bean id="projectProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="projectProperties" /> </bean> <bean id="uniqueAssetIdRetriever" class="com.mypackage.Myclass"> <property name="xml" value="${com.myapp.myproperty1}" /> </bean> 
+3
source

Source: https://habr.com/ru/post/1380813/


All Articles