@ConfigurationProperties
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定,若进行绑定,首先这个类要受Spring管理,spring才能操作里面的数据,有三种使用场景,而通常情况下使用的最多的只是其中的一种场景。本文介绍一下三种场景的使用情况,若有错误还请各位大佬批评指正!
第一种
@Component和@ConfigurationProperties放在bean定义类上。
1、新建application.yml配置文件
person:
id: 1
name: 张三
2、定义一个bean
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person implements Serializable {
private Integer id;
private String name;
}
3、测试
import com.gwm.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringBootConfigTest {
@Autowired
private Person person;
@Test
public void test01(){
System.out.println(person.getId() +"==="+ person.getName());
}
}
4、结果
第二种
@ConfigurationProperties和@Bean注解在配置类的Bean定义方法上。
1、配置文件添加
dataSource:
url: jdbc:mysql://localhost:3306/springboottest?allowMultiQueries=true&serverTimezone=Asia/Shanghai
2、新建配置类
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
// 注意:配置文件中是大小写混合的,prefix中要全部小写,否则会报错
@ConfigurationProperties(prefix = "datasource")
@Bean
public DruidDataSource duidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
3、测试
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringBootConfigTest {
@Autowired
private DruidDataSource druidDataSource;
@Test
public void test02(){
System.out.println(druidDataSource.getUrl());
}
}
4、结果
第三种
@ConfigurationProperties注解到普通类,然后启动类添加@EnableConfigurationProperties定义为Bean。此处以第一种的代码为例,做修改即可。
1、注释@Component注解
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
//@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person implements Serializable {
private Integer id;
private String name;
}
2、启动类新加注解
import com.gwm.pojo.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
// 注解后面要加上开启配置的类----->类名.class
@EnableConfigurationProperties({Person.class})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3、测试
import com.gwm.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringBootConfigTest {
@Autowired
private Person person;
@Test
public void test01(){
System.out.println(person.getId() +"==="+ person.getName());
}
}
4、结果