Developers who don’t understand the Properties in Spring may find it a bit confusing, mainly because there are many configuration methods and many usage methods.
This article is not a principle analysis or source code analysis article, but I hope it can help readers better understand and use Spring Properties.
Use of Properties
All readers of this article have used Spring. Let’s first take a look at how Properties is used. There are several commonly used methods in Spring:
1. Use in the xml configuration file
That is, the value in ${} is automatically replaced.
<bean id="xxx"> <property name="url" value="${javadoop.jdbc.url}" /></bean>2. Use via @Value Injection
@Value("${javadoop.jdbc.url}")private String url;3. Obtain through Environment
There are some things to pay attention to in this method. Not all configuration methods support obtaining property values through the Environment interface. Personal test can only be used when using the annotation @PropertySource, otherwise you will get null. As for how to configure it, I will talk about it immediately below.
@Autowiredprivate Environment env;public String getUrl() { return env.getProperty("javadoop.jdbc.url");}If it is registered with Spring Boot application.properties, that is OK.
Properties Configuration
We mentioned earlier how to use the Properties we configured, so how to configure them? Spring provides many configuration methods.
1. Configure via xml
The following is the most commonly used configuration method, and many projects are written like this:
<context:property-placeholder location="classpath:sys.properties" />
2. Configure via @PropertySource
The previous xml configuration is very common, but if you also have an urge to eliminate all xml configuration files, you should use the following method:
@PropertySource("classpath:sys.properties")@Configurationpublic class JavaDoopConfig {}Note that @PropertySource must be used with @Configuration here, so I won’t go into details.
3. PropertyPlaceholderConfigurer
If readers have seen this, it doesn't matter if they are surprised. This is how it was used before Spring 3.1:
<bean> <property name="locations"> <list> <value>classpath:sys.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> <!-- Some properties can be configured here --></bean>
Of course, we can also use the corresponding 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;}4. PropertySourcesPlaceholderConfigurer
When Spring 3.1 was launched, PropertySourcesPlaceholderConfigurer was introduced, which is a new class. Please note that there is an additional Sources in the name of the previous PropertyPlaceholderConfigurer, and the package it belongs to is different. It is in the Spring-Context package.
There is no difference in configuration:
<bean> <property name="locations"> <list> <value>classpath:sys.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> <!-- Some properties can be configured here --></bean>
Let's also have a 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;}Spring Boot Related
Spring Boot is really a good thing, and it feels so good to use it out of the box. Here is a brief introduction to the relevant content.
Quickly generate a Spring Boot project: https://start.spring.io/
application.properties
Each project has an application.properties file by default. This configuration file does not need to be registered as mentioned earlier. Spring Boot will help us automatically register.
Of course, maybe you can change the name, just specify your file name when starting:
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 of the test environment and the production environment is different.
Therefore, based on application.properties, we also need to create new application-dev.properties and application-prd.properties to configure environment-related information, and then specify the environment when starting.
java -Dspring.profiles.active=prd -jar app.jar
The result is that the configurations in both application.properties and application-prd.properties files will be registered. If there are duplicate keys, the priority in the application-prd.properties files is higher.
@ConfigurationProperties
This annotation is only available in Spring Boot.
Even if you don’t use this annotation, you may see this in an open source project. Here is a brief introduction.
Let's take an example to be more intuitive. As mentioned before, fill in the following information in the configuration file, you can choose to write to application.properties or use the method introduced in the first section.
javadoop.database.url=jdbc:mysql:javadoop.database.username=adminjavadoop.database.password=admin123456
java file:
@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 Spring container, and the properties are set.
Dynamically modify attribute values during startup
I don't think this needs too much introduction, and those who use Spring Boot should basically know it.
The attribute configuration has an override order, that is, when the same key appears, the value shall prevail.
Startup Parameters > application-{env}.properties > application.properties
Start parameters dynamically set properties:
java -Djavadoop.database.password=admin4321 -jar app.jar
In addition, you can also use system environment variables to set properties, specify random numbers, etc., which is indeed very flexible, but it is useless, so I won't introduce it.
Summarize
If readers want to have a deeper understanding of Spring Properties, they need to understand the source code related to Spring Environment interface. Interested readers are advised to search through the source code.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.