Several ways of SpringBoot configuration file value injection

When using the springboot project to develop, we prefer to configure it in a special file, such as database configuration information, so that the release can facilitate operation and maintenance for maintenance. The following methods are provided for everyone to learn.

一、@ConfigurationProperties

Function: Tell SpringBoot to bind all the attributes in this class to the related configuration in the configuration file;

This is my other blog, which explains the basic usage of this annotation.

Link: Related explanations .

Two, @Value

For application.properties, it is also possible to use a yml file.
Fill in the database link information here

datasource.url=123
datasource.username=456
datasource.password=789
datasource.gw-url=127.0.0.1

The table that stores the database information. Here we inject it into the container and take it directly from the container when it is used.

@Component
public class DruidDBConfig {
    
    
	@Value("${datasource.url}")  
    private String url;  
    
    @Value("${datasource.username}")  
    private String username;  
    
    @Value("${datasource.password}")  
    private String password; 
    
    @Value("${datasource.gw-url}")  
    private String gwUrl;
}

The junit test successfully obtained the value injected by the configuration file.
Insert picture description here
The actual principle is that, just like the bean in the xml, it relies on the set method to assign the bean value and store it in the container.

<bean class="Person">
     <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
<bean/>

Three, the difference between @ConfigurationProperties and @Value binding

1. Loose binding:

This refers to the hump naming.
If you use the @ConfigurationProperties method: person.last-name written in properties, and the property name in java private String lastName; written in this way, you can still inject values, and @Value annotation is not.

2. SpEL support

SpEL (Spring Expression Language), the Spring Expression Language, is an expression language that is more powerful than JSP's EL. Why sum up SpEL, because it can query and manipulate data at runtime, especially array list data, so it can reduce the amount of code and optimize the code structure.

@Value("#{表达式}")
public String arg;

@Value("#{11*2}")
private Integer age;

3. JSR303 data verification

JSR-303 is a sub-specification in JAVA EE 6, called Bean Validation, and the official reference implementation is Hibernate Validator.
This implementation has nothing to do with Hibernate ORM. JSR 303 is used to verify the value of a field in a Java Bean.

Add the @Validated annotation to the class and then enable the validation annotation. Then we can use some of his special annotations, such as @Email

4. Complex type packaging

@ConfigurationProperties can assign values ​​to collections such as map and list, while @Value is not.

5. Summary

Insert picture description here

Three, @PropertySource

Function: Load the specified configuration file;
the two annotations mentioned above are injection values, and the values ​​are configured in the application main configuration file before they can be injected.
We can use @PropertySource to configure some fixed information into a configuration file.
It can be used with @ConfigurationProperties and @Value.
Detailed usage:
create DruidDBConfig.properties

datasource.url=123
datasource.username=456
datasource.password=789
datasource.gw-url=127.0.0.1
package com.atguigu.springboot.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = {
    
    "classpath:DruidDBConfig.properties"})
public class DruidDBConfig {
    
    
	
	
	@Value("${datasource.url}")  
    private String url;  
    
    @Value("${datasource.username}")  
    private String username;  
    
    @Value("${datasource.password}")  
    private String password; 
    
    @Value("${datasource.gw-url}")  
    private String gwUrl;

	@Override
	public String toString() {
    
    
		return "DruidDBConfig [url=" + url + ", username=" + username + ", password=" + password + ", gwUrl=" + gwUrl
				+ "]";
	}
}

The junit test can still get the value
Insert picture description here

Four, @ImportResource

Some friends always confuse the difference between him and @PropertySource during use.

@ImportResource Function: Import the Spring configuration file to make the content in the configuration file take effect;

There is another similar to this annotation, that is @Import, he can directly load the class configuration file, and the class loaded with this annotation will be directly stored in the IOC container.

@ImportResource can be used to load xml configuration files

If you want Spring's xml configuration file to take effect, load it in; @ImportResource is marked on a configuration class, and the startup class is also possible.

@ImportResource(locations = {
    
    "classpath:beans.xml"}) 
导入Spring的配置文件让其生效

Write Spring configuration file

<?xml version="1.0" encoding="UTF‐8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring‐beans.xsd"> 
	<bean id="helloService" class="com.gzl.springboot.service.HelloService"></bean> 
</beans>

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/110943294