@ConfigurationProperties
是 SpringBoot 提供的重要注解,可以将一些配置属性批量注入到 bean 对象。
方式一:@Value一个个注入
1、application.yml
配置文件:
spring:
jdbc:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///springboot_01
username: root
password: root
2、自动配置类DataSourceProperties
:
package org.example.properties;
@Component
public class DataSourceProperties {
@Value("${spring.jdbc.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.jdbc.datasource.url}")
private String url;
@Value("${spring.jdbc.datasource.username}")
private String username;
@Value("${spring.jdbc.datasource.password}")
private String password;
@Override
public String toString() {
return "DataSourceProperties{" +
"driverClassName='" + driverClassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3、Controller 调用:
package org.example.controller;
...
@RestController
public class HelloController {
@Autowired
private DataSourceProperties dataSourceProperties;
@RequestMapping("/hello")
public String hello(){
System.out.println(dataSourceProperties);
return "hello spring boot!!" ;
}
}
4、访问 http://localhost:8080/hello
,输出:
DataSourceProperties{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///springboot_01', username='root', password='root'}
@Value
注入方式,如果属性特别多,一个一个注入太麻烦啦!
方式二:@ConfigurationProperties批量注入
1、自动配置类DataSourceProperties2
:
package org.example.properties;
...
@Component
@ConfigurationProperties("spring.jdbc.datasource")
public class DataSourceProperties2 {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() { return driverClassName; }
public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; }
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
@Override
public String toString() {
return "DataSourceProperties{" +
"driverClassName='" + driverClassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
@ConfigurationProperties
注解声明该类要读取属性配置prefix="spring.jdbc.datasource"
读取属性文件中前缀为spring.jdbc.datasource
的值。前缀和属性名称和配置文件中的 key 必须要保持一致才可以注入成功- SpringBoot 默认读取 application.properties 属性文件
2、Controller 调用:
@RestController
//@EnableConfigurationProperties(DataSourceProperties2.class)
public class HelloController {
@Autowired
private DataSourceProperties dataSourceProperties;
@Autowired
private DataSourceProperties2 dataSourceProperties2;
@RequestMapping("/hello")
public String hello(){
System.out.println(dataSourceProperties);
return "hello spring boot!!" ;
}
@RequestMapping("/hello2")
public String hello2(){
System.out.println(dataSourceProperties2);
return "hello spring boot!!" ;
}
}
@EnableConfigurationProperties(DataSourceProperties2.class)
注解说明注入 DataSourceProperties2 类包含的属性,方便追溯代码。 注:该注解不加,也可以…囧
3、pom文件添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
4、访问 http://localhost:8080/hello2
,输出:
DataSourceProperties{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///springboot_01', username='root', password='root'}
@ConfigurationProperties
注入方式,属性再多,只要按照规则就可以一次性自动注入。方便很多!
方式三:对象属性注入
@ConfigurationProperties
不仅可以自动注入简单类型数据,也支持对象属性注入。
1、application.yml 配置文件新增 type:
spring:
jdbc:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///springboot_01
username: root
password: root
type: mysql
2、自动配置类DataSourceProperties3
:
package org.example.properties;
...
@Component
@ConfigurationProperties(prefix = "spring.jdbc")
public class DataSourceProperties3 {
private DataSourceProperties2 dataSource; //注意,属性名称要和application.yml保持一致。
private String type ;
public DataSourceProperties2 getDataSource() {
return dataSource;
}
public void setDataSource(DataSourceProperties2 dataSource) {
this.dataSource = dataSource;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "DataSourceProperties3{" +
"dataSource=" + dataSource +
", type='" + type + '\'' +
'}';
}
}
private DataSourceProperties2 dataSource;
属性名称 dataSource 要和 application.yml 保持一致,才可以注入成功。当然,也可以添加@Autowired
注解,这样保证 DataSourceProperties2 对象的注入,也可以保证配置注入成功。
@Component
@ConfigurationProperties(prefix = "spring.jdbc")
public class DataSourceProperties3 {
@Autowired
private DataSourceProperties2 source;
private String type ;
...
}
3、Controller 调用:
@RestController
public class HelloController {
@Autowired
private DataSourceProperties3 dataSourceProperties3;
@RequestMapping("/hello3")
public String hello3(){
System.out.println(dataSourceProperties3);
return "hello spring boot!!" ;
}
}
4、访问 http://localhost:8080/hello3
,输出:
DataSourceProperties3{dataSource=DataSourceProperties{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///springboot_01', username='root', password='root'}, type='mysql'}
方式四:外部对象属性注入
场景:现有一个类DataSource
,需要将配置文件中的内容注入其中。
package org.example.properties;
public class DataSource {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {return driverClassName;}
public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}
public String getUrl() {return url;}
public void setUrl(String url) {this.url = url;}
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}
@Override
public String toString() {
return "DataSource{" +
"driverClassName='" + driverClassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
这种情况,需要将该配置类添加到Spring Context中,该怎么做呢???
@Bean
和@Component
都是将 Spring Bean 添加到 Spring Context 中。
因为该配置类没有 @Component 修饰,我们使用 @Bean 将该类添加到 Spring Context 中。
1、新建 JdbcConfig
配置类:
package org.example.config;
import org.example.properties.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JdbcConfig {
@Bean
@ConfigurationProperties(prefix = "spring.jdbc.datasource")
public DataSource getDataSource(){
return new DataSource();
}
}
@ConfigurationProperties(prefix = "spring.jdbc.datasource")
修饰返回的类,将会对返回的对象自动注入属性。
2、自动配置类JdbcProperties
:
package org.example.properties;
...
@Component
@ConfigurationProperties(prefix = "spring.jdbc")
public class JdbcProperties {
@Autowired
private DataSource source; //注意,属性名称要和application.yml保持一致。
private String type ;
public DataSource getSource() {
return source;
}
public void setSource(DataSource source) {
this.source = source;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "JdbcProperties{" +
"dataSource=" + source +
", type='" + type + '\'' +
'}';
}
}
3、Controller 调用:
@RestController
public class HelloController {
@Autowired
private JdbcProperties jdbcProperties;
@RequestMapping("/hello4")
public String hello4(){
System.out.println(jdbcProperties);
return "hello spring boot!!" ;
}
}
4、访问 http://localhost:8080/hello4
,输出:
JdbcProperties{dataSource=DataSource{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///springboot_01', username='root', password='root'}, type='mysql'}