Developers who do not understand the Properties in Spring may feel a bit messy, mainly because there are many ways to configure them, and there are many ways to use them.
This article is not a principle analysis, source code analysis article, just hope to help readers better understand and use Spring Properties.
Use of Properties
Readers of this article have used spring, first of all to see how the Properties are used, Spring is commonly used in the following ways:
1. Using in the XML configuration file
That is, automatically replace the value inside the ${}.
<bean id= "xxx" class= "com.javadoop.Xxx" >
<property name= "url" value= "${javadoop.jdbc.url}"/>
</bean>
2. Using @Value Injection
@Value ("${javadoop.jdbc.url}")
Private String URL;
3. Get through environment
There is a need to pay attention to this method. Not all configuration methods are supported through the environment interface to obtain property values, pro-test only when using annotations @PropertySource can be used, otherwise you will get null, as to how to configure, the following will be said immediately.
@Autowired
Private environment env;
Public String GetUrl () {
Return Env.getproperty ("Javadoop.jdbc.url");
}
If it is a Spring Boot application.properties registered, it is also possible.
Properties Configuration
We said before how to use the Properties of our configuration, then how to configure it? Spring provides many ways to configure it.
1. Configuring with XML
The following is the most commonly used configuration, many projects are written in this way:
<context:property-placeholder location= "Classpath:sys.properties"/>
2. Through @PropertySource configuration
The preceding XML configuration is very common, but if you have an impulse to destroy all of the XML configuration files, you should use the following method:
@PropertySource ("Classpath:sys.properties")
@Configuration
public class Javadoopconfig {
}
Note that @PropertySource must be used here with @Configuration, specifically not to expand.
3. Propertyplaceholderconfigurer
If the reader has seen this, it is not surprising that it is often used before Spring 3.1:
<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>
Of course, we can also use the corresponding Java configuration version:
@Bean
Public Propertyplaceholderconfigurer propertiess () {
Propertyplaceholderconfigurer PPC = new Propertyplaceholderconfigurer ();
resource[] Resources = new Classpathresource[]{new Classpathresource ("Sys.properties")};
Ppc.setlocations (resources);
Ppc.setignoreunresolvableplaceholders (TRUE);
return PPC;
}
4. Propertysourcesplaceholderconfigurer
By the time Spring 3.1 was introduced, Propertysourcesplaceholderconfigurer, a new class, was noticed and the previous propertyplaceholderconfigurer had one more name on it. Sources, the owning package is not the same, it is in the Spring-context package.
There's no difference in configuration:
<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>
Let's also have a Java configuration version:
@Bean
Public Propertysourcesplaceholderconfigurer properties () {
Propertysourcesplaceholderconfigurer PSPC = new Propertysourcesplaceholderconfigurer ();
resource[] Resources = new Classpathresource[]{new Classpathresource ("Sys.properties")};
Pspc.setlocations (resources);
Pspc.setignoreunresolvableplaceholders (TRUE);
return PSPC;
}
Spring Boot Related
Spring Boot is really a good thing, and it's great to be out of the box. Here is a brief description of the relevant content.
Quickly generate a Spring Boot project: https://start.spring.io/
Application.properties
Each of our projects has a application.properties file by default, which does not need to be registered as previously stated, and Spring Boot will automatically register it for us.
Of course, you may want to change a name is also possible, at the start of the time to specify your file name can be:
Java-dspring.config.location=classpath:sys.properties-jar App.jar
Application-{env}.properties
In order to specify different configurations for different environments, we will use this.
For example, the database connection information for the test environment and the production environment is different.
So, on the basis of application.properties, we also need to create new application-dev.properties and Application-prd.properties is used to configure environment-related information and then to specify the environment when it is started.
Java-dspring.profiles.active=prd-jar App.jar
As a result, the configuration in Application.properties and application-prd.properties two files is registered, if there is a duplicate key,application-prd.properties Higher priority in the file.
@ConfigurationProperties
This annotation is only in Spring Boot.
Even if you do not use this annotation, you will probably see this in open source projects, here is a brief introduction.
Come up with an example that is intuitive. As previously stated, fill in the following information in the configuration file, you can choose to write application.properties can also use the method described in the first section.
Javadoop.database.url=jdbc:mysql:
Javadoop.database.username=admin
javadoop.database.password=admin123456
Java files:
@Configuration
@ConfigurationProperties (prefix = "javadoop.database")
public class DataBase {
String URL;
String username;
String password;
Getters and Setters
}
In this way, a bean of type DataBase is automatically registered in the container of Spring, and the properties are set well.
Dynamically modifying property values during startup
I don't think I need a lot of introductions, and the Spring Boot should basically know.
The property configuration has a overwrite order, that is, when the same key appears, the value of where to prevail.
Startup parameters > Application-{env}.properties > Application.properties
Startup parameters Dynamic Setting properties:
Java-djavadoop.database.password=admin4321-jar App.jar
In addition, you can also use the system environment variables to set properties, you can specify random numbers and so on, it is very flexible, but no use, not introduced.
Summarize
If you want to know more about the Properties of spring, you need to understand the source code associated with spring's environment interface. Suggest interested readers to turn over the source code to see.
The ubiquitous Properties in Spring