SpringBoot之获取配置文件内容

1介绍

2@ConfigurationProperties注解获取全局配置文件中内容

举例说明:
yml文件

person:
	lastName: zhangsan
	age: 18
	boss: false
	birth: 2017/12/12
	maps: { k1: v1, k2: v2 }
	list:
		- lisi
		- wangwu
	dog:
		name: 小狗
		age: 2

在java类中读取全局配置文件中的配置
@ConfigurationProperties注解的prefix属性,匹配application.yml全局配置文件中的一个属性值,然后映射到这个组件。

#注意,配置文件的赋值,是通过成员属性对应的setter方法。
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {

    private String lastName;
    private int age;
    private boolean boss;
    private Date birth;
    private Map maps;
    private List list;
    private Dog dog;
   

注意:
1、出现如下错误:spring boot configuration annotation processor not configured
在这里插入图片描述在pom.xml中增加如下依赖

<dependency>
	<groupId>org.springframe.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

导入配置文件处理器,配置文件进行绑定时就会有提示。

3@Value注解获取全局配置文件中内容

举例说明

test:
  age: 2
  price: 3.14
  isOk: true
  birth: 2018/12/12
  date: 2019/11/11 21:59:43
  greet: 'hello \n world'
  greet1: 'hello\n world'

在java类中读取全局配置文件中的配置

@Component
@Data
public class Test {

    @Value("${test.age}")
    private int age;

    @Value("${test.price}")
    private float price;

    @Value("${test.birth}")
    private Date birth;

    @Value("${test.date}")
    private Date date;

    @Value("${test.greet}")
    private String greet;

    @Value("${test.greet1}")
    private String greet1;
    
}

4@Value与@ConfigurationProperties对比

在这里插入图片描述

5使用 @PropertySource 读取其他的Properties配置文件

使用@Value进行赋值

#1.在source目录下创建dataSource.properties
username = root
password = 123123
dirverClassName = org.git.mm.mysql.Driver
url = jdbc:mysql://localhost;3306/test


#2.DataSource.java中配置,使用@Value获取datasource.properties中的内容
@Data
@PropertySource(value ="classpath:datasource.properties")
@Component(value = "dataSource")
public class DataSource {

    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;
    @Value("${url}")
    private String url;
    @Value("${dirverClassName}")
    private String driverClassName;
}

使用@ConfigurationProperties进行赋值

#1.在source目录下创建dataSource.properties.
datasource.username = root
datasource.password = 123123
datasource.dirver-class-name = org.git.mm.mysql.Driver
datasource.url = jdbc:mysql://localhost;3306/test


#2.DataSource.java中配置,使用@ConfigurationProperties(prefix="")获取datasource.properties中的内容.
@Data
@PropertySource(value ="classpath:datasource.properties")
@ConfigurationProperties(prefix = "datasource")
@Component(value = "dataSource")
public class DataSource {

    private String username;

    private String password;

    private String url;

    private String driverClassName;
}

参考

springboot教程 yml配置文件 与 获取配置文件中的内容:https://blog.csdn.net/qq_36723759/article/details/104254275

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weightOneMillion

感谢未来的亿万富翁捧个钱场~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值