We can define the relevant properties of the Spring Boot project in the application.properties file. Of course, these related properties can be system properties, environment variables, command parameters and other information, or the name and location of the custom configuration file. Next, we will demonstrate a custom configuration property, taking the custom database information as an example
1. Use @ Value() annotation to complete attribute injection
(1) First, the data source connection dependency is introduced
<dependency> <groupId>com.github.drtrang</groupId> <artifactId>druid-spring-boot2-starter</artifactId> <version>1.1.10</version> </dependency>
(2) Write application.yml
jdbc: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/demo username: root password: root
(3) Create data source entity class
@Data @Configuration public class JdbcConfiguration { @Value("${jdbc.url}") String url; @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
(4) Test data
It is clear that the value in the configuration file is obtained through the @ Value() annotation
2. Batch injection using @ ConfigurationProperties
(1) Modify data source entity class
@Data @Configuration @EnableConfigurationProperties @ConfigurationProperties(prefix = "jdbc") public class JdbcConfiguration { private String url; private String driverClassName; private String username; private String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
@ Configuration: indicates that this class is a configuration class
@ EnableConfigurationProperties: enables the @ ConfigurationProperties annotation
@ ConfigurationProperties(prefix = "jdbc") : Pass the attribute values starting with JDBC in the configuration file The setXX() method is injected into the corresponding attribute of the entity class
In order to prompt when writing custom properties in development and to facilitate configuration with the effect of code prompt, when injecting the property value of the configuration file with the @ ConfigurationProperties annotation, you can add a configuration handler dependency provided by Spring Boot to the pom.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
(2) Writing yml configuration files
jdbc: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/demo username: root password: root
(3) View effect
View effect description@ ConfigurationProperties can also implement the operation of property injection, and supports batch injection without operating on each property.
3. Inject third-party configuration
(1) Create another component class (here as a third-party class)
@Data public class User { private String userName; private String pwd; }
(2) Create configuration classes for files
@Configuration public class MyUserConfiguration { @ConfigurationProperties(prefix = "user") @Bean public User user(){ return new User(); } }
(3) Write configuration file
user: userName: zs pwd: 123
(4) Write test
@SpringBootTest class DemoApplicationTests { @Autowired private User user; @Test void test() { System.out.println(user); } }
(5) View effect
4. Loose binding
Spring Boot uses some loose rules to bind environment properties to @ configurationproperties beans, so there is no need for an exact match between environment property names and bean property names
For example, in the above case, we modify the configuration file
user: user_name: zs pwd: 123
View effect
Discovery can also bind successfully.
The supported loose binding methods when binding with @ ConfigurationProperties are
Mutton kebab mode case, Recommended: user.user-name
Standard hump mode: user.userName
Underline mode: user.user_name
Uppercase underline. It is recommended to use: user when using the system environment_ USER_ NAME
Summary:
@Configuration: declare a class as a configuration class @ Bean: declare on the method and add the return value of the method to the bean container
@Value: Property injection
@ConfigurationProperties(prefix = "jdbc"): batch property injection
@PropertySource("classpath:/jdbc.properties") specifies the external properties file. Add on class