Use and configuration methods of Spring Properties

  • 2020-12-10 00:44:29
  • OfStack

Developers who don't understand Properties in Spring may find it a little confusing, mainly because there are so many ways to configure and use Properties.

This article is not a principle analysis, source analysis article, just hope to help readers better understand and use Spring Properties.

The use of Properties

Readers of this article have used Spring before. Let's first take a look at how Properties is used. There are several common ways to use Spring:

1. Use in the xml configuration file

Replace the value in ${} automatically.


<bean id="xxx" class="com.javadoop.Xxx">
   <property name="url" value="${javadoop.jdbc.url}" />
</bean>

2. Use through @Value injection


@Value("${javadoop.jdbc.url}")
private String url;

3. Via Environment

There are some points which need attention in this method. Not all configuration methods support getting attribute values through the Environment interface. The test can only be used with the annotation @PropertySource, otherwise it will get null. How to configure it will be explained in a moment.


@Autowired
private Environment env;

public String getUrl() {
  return env.getProperty("javadoop.jdbc.url");
}

If it is registered with ES38en. properties of Spring Boot, it is also ok.

Properties configuration

We talked earlier about how to use Properties as we configured it, so how do you configure it? Spring provides a number of configuration options.

1. Configuration via xml

This is the most common configuration, as many projects write:


<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 kill all xml configuration files, you should use the following:


@PropertySource("classpath:sys.properties")
@Configuration
public class JavaDoopConfig {
}

Note that at one point, @PropertySource must be used with @Configuration, so I won't go into details.

3. PropertyPlaceholderConfigurer

If you've seen this, don't be surprised, as it was 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"/>
   <!--  I can configure it here 1 Some properties  -->
</bean>

Of course, we can also use the corresponding version of java configuration:


@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

At the time of Spring 3.1, PropertySourcesPlaceholderConfigurer is introduced, which is a new class. Notice that there is an extra Sources in the name of the previous PropertyPlaceholderConfigurer, and the package is not the same, it is in the PACKAGE of ES86en-ES87en.

There is 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"/>
  <!--  I can configure it here 1 Some properties  -->
</bean>

Have a version of java configuration:


@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 good, it feels so good out of the box. Here's a brief introduction.

Quickly generate a Spring Boot project: https: / / start spring. io /

application.properties

By default we have one ES117en.properties file for each project. This configuration file does not need to be registered as mentioned earlier. Spring Boot will help us to register automatically.

Of course, if you want to change the name, you can just specify the name of your file at startup:


java -Dspring.config.location=classpath:sys.properties -jar app.jar

application-{env}.properties

We will use this to specify different configurations for different environments.

For example, the database connection information of test environment and production environment is not the same.

So, on top of application.properties, we need to create new ES135en-ES136en.properties and ES138en-ES139en.properties to configure the environment-related information, and then specify the environment on startup.


@Value("${javadoop.jdbc.url}")
private String url;
0

As a result, configurations in both application.properties and ES146en-ES147en.properties files are registered, and if there is a duplicate key, application-ES151en.properties files have a higher priority.

@ConfigurationProperties

This annotation is unique to Spring Boot.

Even if you don't use this annotation, you might see it in an open source project, but here's a brief introduction.

Let me do an example just to make it a little bit more intuitive. As mentioned earlier, fill in the following information in the configuration file. You can choose to write application.properties or use the method described in Section 1.


@Value("${javadoop.jdbc.url}")
private String url;
1

java file:


@Value("${javadoop.jdbc.url}")
private String url;
2

This way, an bean of type DataBase is automatically registered in the Spring container, and the attributes are all set ready.

Property values are dynamically changed during startup

I don't think I need much introduction to this, but I think most of you know Spring Boot.

The property configuration has an override order, which is where the value will be when the same key occurs.

Launch parameters > application-{env}.properties > application.properties

Dynamic setting of startup parameters Properties:


java -Djavadoop.database.password=admin4321 -jar app.jar

In addition, you can also use system environment variables to set properties, you can also specify random numbers and so on, it is very flexible, but not useful, I will not introduce.

conclusion

To gain a deeper understanding of Spring's Properties, readers need to understand the source code associated with Spring's Environment interface. Interested readers are encouraged to browse the source code


Related articles: