【springboot读取配置文件】@ConfigurationProperties、@PropertySource和@Value
概念:
@ConfigurationProperties : 是springboot的注解,用于把主配置文件中配置属性设置到对于的Bean属性上
@PropertySource :是spring的注解,用于加载指定的属性配置文件到Spring的Environment中,可以和 @Value、@ConfigurationProperties配合使用
@EnableConfigurationProperties : 用来开启ConfigurationProperties注解配置;如果不使用的话,@ConfigurationProperties加入注解的类上加@Component也是可以交于springboot管理。
1、读取默认配置文件(application.properties、application.yml)
application.yml配置:

实现方式一 @ConfigurationProperties + @Component作用于类上
@ConfigurationProperties(prefix="person")
@Componment
@Data // lombok,用于自动生成getter、setter
public class Person {
private String name;
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
实现方式二 @ConfigurationProperties + @Bean作用在配置类的bean方法上
@Data
public class Person {
private String name;
}
@Configuration
public class PersonConf{
@Bean
@ConfigurationProperties(prefix="person")
public Person person(){
return new Person();
}
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
实现方式三 @ConfigurationProperties注解到普通类、 @EnableConfigurationProperties注解定义为bean
@ConfigurationProperties(prefix="person")
@Data
public class Person {
private String name;
}
// 说明: @EnableConfigurationProperties可以直接注到启动类上,也可以放在自定义配置类,自定义配置类使用@Configuration标注
@SpringBootApplication
@EnableConfigurationProperties(Person.class)
public class DbApplication {
public static void main(String[] args) {
SpringApplication.run(DbApplication.class, args);
}
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
实现方式四 @Value作用属性上
@RestController
@RequestMapping("/db")
public class TestController {
@Value("${person.name}")
private String name;
@GetMapping("/person")
public String parsePerson() {
return name;
}
}
实现方式五 使用自带的Environment对象
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Environment environment;
@GetMapping("/person")
public String parsePerson() {
return environment.getProperty("person.name");
}
}
2、读取自定义配置文件(比如:dangxiaodang.properties)
dangxiaodang.properties配置:(说明: PropertySource不支持yml、yaml,详细请看扩展内容)

实现方式一 @Configuration + @PropertySource + Environment
@Data
public class Person {
private String name;
}
@Configuration
@PropertySource(value = "classpath:dangxiaodang.properties")
public class PersonConf {
@Autowired
private Environment environment;
@Bean
public Person person(){
Person person = new Person();
person.setName(environment.getProperty("person.name"));
return person;
}
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
实现方式二 @Configuration + @PropertySource + @Value
@Component
@PropertySource(value = "classpath:dangxiaodang.properties")
@Data
public class Person {
@Value("${person.name}")
private String name;
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
实现方式三 @Configuration + @PropertySource + @ConfigurationProperties
@Component
@PropertySource("classpath:dangxiaodang.properties")
@ConfigurationProperties(prefix = "person")
public class Person{
private String name;
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
扩展内容
- 分析:@PropertySource 的注解中,有一个factory属性,可指定一个自定义的PropertySourceFactory接口实现,用于解析指定的文件。其中默认的实现是DefaultPropertySourceFactory,继续跟进,使用了PropertiesLoaderUtils.loadProperties进行文件解析,所以默认就是使用Properties进行解析的。
- 解决方案:
- 自定义实现类实现PropertySourceFactory
- 自定义类继承DefaultPropertySourceFactory
- 实现代码:
dangxiaodang.yaml配置文件:(yml也支持)

public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
Properties ymlProperties = factory.getObject();
String propertyName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(propertyName, ymlProperties);
}
}
@Component
@PropertySource(value = "classpath:dangbo.yml", factory = YamlPropertySourceFactory.class) // 指定对应的factory
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
private String name;
}
@RestController
@RequestMapping("/db")
public class TestController {
@Autowired
private Person person;
@GetMapping("/person")
public String parsePerson() {
return person.getName();
}
}
【springboot读取配置文件】@ConfigurationProperties、@PropertySource和@Value的更多相关文章
- SpringBoot读取配置文件源码探究
1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...
- springboot读取配置文件中的信息
在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...
- SpringBoot读取配置文件的内容
1.@Value读取 在springboot项目中,如果要读取配置文件application.properties或application.yml文件的内容,可以使用自带的注解@Value.以prop ...
- Springboot读取配置文件的两种方法
第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...
- SpringBoot 读取配置文件的值 赋给静态变量
需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...
- springboot 读取配置文件
读取配置文件 在以前的项目中我们主要在 XML 文件中进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息. 在 Spring Boot 中我们不再需要使用这种方式 ...
- SpringBoot读取配置文件三步走
1首先新建application.properties文件 cn.qdl.demo.url=http://localhost:8080 2写一个类包上面的配置文件,类名随便取 public class ...
- springboot读取配置文件的顺序
前言 今天测试一些东西,发现配置文件连接的数据库一直不正常,数据也不对,今天请教了之后,原来springboot的配置文件加载不仅仅是项目内的配置文件. 正文 项目目录是这样的:文件夹下有:项目,ap ...
- Springboot读取配置文件及自定义配置文件
1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...
随机推荐
- GeoMesa,整体架构,创建Schema并导入数据
GeoMesa,整体架构,创建Schema并导入数据 一.GeoMesa-整体架构 二.GeoMesa-创建Schema并导入数据 2.1 GeoTools Data 模块 2.2 索引管理 2.3 ...
- orm(Manager isn't accessible via %s instances" % cls.__name)报错
报错信息 Manager isn't accessible via %s instances" % cls.__name 解决方法 https://www.jianshu.com/p/5e0 ...
- 调用ajax 跨域调用接口
//ajax 跨域请求数据 function ajaxType (){ $.ajax({ url: "http://127.0.0.1:9090/spring_mvc/HttpClient/ ...
- FortiGate防火墙办公网常用配置
1.建立主备机 2.配置端口 3.配置SD-WAN 4.新建上网路由 5.新建上网策略 6.建立与其他点的IPSec VPN或点对点专线
- codeforces629C Famil Door and Brackets (dp)
As Famil Door's birthday is coming, some of his friends (like Gabi) decided to buy a present for him ...
- hdu 6795 Little W and Contest 并查集+排列组合
题意: t组输入,有n个人,刚开始谁也不认识谁.每一个人有一个权值w[i](1<=w[i]<=2),你要挑选3个互相不认识的人组成一个队,且要保证3个人权值之和大于等于5(也就意味着最少要 ...
- python给字段名和值都加上引号
import re c = ''' Accept: application/json, text/javascript, */*; q=0.01 Accept-Encoding: gzip, defl ...
- 仿ATM程序软件
一.目标: ATM仿真软件 1 系统的基本功能 ATM的管理系统其基本功能如下:密码验证机制:吞锁卡机制:存取款功能:账户查询功能:转账功能等. 要求 要能提供以下几个基本功能: (1)系统内的相关信 ...
- hdu5414 CRB and String
Problem Description CRB has two strings s and t. In each step, CRB can select arbitrary character c ...
- Spring:解决因@Async引起的循环依赖报错
最近项目中使用@Async注解在方法上引起了循环依赖报错: org.springframework.beans.factory.BeanCurrentlyInCreationException: Er ...