Developers who don’t understand 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 just hope it can help readers better understand and use Spring Properties.
Use of Properties
All readers of this article have used Spring. Let’s take a look at how Properties are 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 class=""> <property name="url" value="${}" /> </bean>
2. Use via @Value Injection
@Value("${}") 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.
@Autowired private Environment env; public String getUrl() { return (""); }
If it is registered by Spring Boot, that's 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:" />
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:") @Configuration public 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 were surprised. This was often used before Spring 3.1:
<bean class=""> <property name="locations"> <list> <value>classpath:</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("")}; (resources); (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 class=""> <property name="locations"> <list> <value>classpath:</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("")}; (resources); (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:/
Each project has a 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 -=classpath: -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, on the basis of , we also need to create and , which is used to configure the environment-related information, and then specify the environment when starting.
java -=prd -jar
The result is that the configurations in both files and will be registered. If there are duplicate keys, the priority in the file 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 it or use the method introduced in the first section.
=jdbc:mysql: =admin =admin123456
java file:
@Configuration @ConfigurationProperties(prefix = "") 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 >
Start parameters dynamically set properties:
java -=admin4321 -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's Properties, they need to understand the source code related to Spring's 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 study and I hope everyone will support me more.