Properties Injection
- Through XML configuration
- by @PropertySource Configuration
- Propertyplaceholderconfigurer
- Propertysourcesplaceholderconfigurer
Use of Properties
- Using in an XML configuration file
- Using @Value Injection
- Get through environment
- Get through Application.properties
Spring Boot Related
- @ConfigurationProperties
- Configuration priority
The Properties configuration is configured through XML
<context:property-placeholder location ="classpath:sys.properties" />
by @PropertySource Configuration
@PropertySource ("Classpath:sys.properties") @Configurationpublic class Democonfig {}
@PropertySource must be used in conjunction with @Configuration here.
Propertyplaceholderconfigurer
<bean class= "Org.springframework. beans.factory . config. Propertyplaceholderconfigurer "> <property name=" Locations "> <list> <value> classpath:sys.properties</value> </list> </property> <property name= " Ignoreunresolvableplaceholders "value=" true "/> <!--Here you can configure some properties--></bean>
Java Configuration version
@Beanpublic propertyplaceholderconfigurer propertiess () { Propertyplaceholderconfigurer PPC = new Propertyplaceholderconfigurer (); resource[] Resources = new Classpathresource[]{new Classpathresource ("Sys.properties")}; Ppc.setlocations (resources); Ppc.setignoreunresolvableplaceholders (true); return PPC;}
Propertysourcesplaceholderconfigurer
<bean class= "Org.springframework. Context.support. Propertysourcesplaceholderconfigurer "> <property name=" Locations "> <list> <value >classpath:sys.properties</value> </list> </property> <property name= " Ignoreunresolvableplaceholders "value=" true "/> <!--Here you can configure some properties--></bean>
Java Configuration version
@Beanpublic Propertysourcesplaceholderconfigurer Properties () { Propertysourcesplaceholderconfigurer pspc = new Propertysourcesplaceholderconfigurer (); resource[] Resources = new Classpathresource[]{new Classpathresource ("Sys.properties")}; Pspc.setlocations (resources); Pspc.setignoreunresolvableplaceholders (true); return PSPC;}
Use of Properties in an XML configuration file
<bean id= "xxx" class= "com.demo.Xxx" > <property name= "url" value= "${mysql.jdbc.url}"/></bean>
Using @Value Injection
@Value ("${demo.jdbc.url}") Private String URL;
Get through environment
It can be used only when using annotations @PropertySource, otherwise it will be null.
@Autowiredprivate Environment Env;public String getUrl () { return Env.getproperty ("Demo.jdbc.url");}
Get through Application.properties
Demo.database.url=jdbc:mysql:
Spring Boot Related @configurationproperties
Application.properties
Demo.db.url=jdbc:mysql:demo.db.username=testdemo.db.password=123456@configuration@configurationproperties ( prefix = "demo.db") @Datapublic class DataBase { String URL; String username; String password;}
Configuration priority
Java -dspring.profiles.active=env -jar App.jar
If there are two files,application.properties and application-env.properties , then the configuration in the two files will be registered, if there are duplicate keys, Higher priority in the Application-env.properties file.
Summary: startup parameters > application-{env}.properties > application.properties
Properties in Spring