springBoot的各种注解

1 @RefreshScope 

作用:文件自动刷新

 

2@ConfigurationProperties

作用:读取文件的配置信息

配置文件:

connection.username=admin
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1
@Setter
@Getter
@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {

    private String username;
    private String remoteAddress;
    private String password ;}

3几种配置文件的对比

(1)添加properties文件

info:

   name: xiaoming

    age: 13 sex: 1

1@value读取

@Setter
@Getter

@Component
public class TechUser {
    @Value("${info.name}")
    private String name;
    @Value("${info.age}")
    private int age;
    @Value("${info.sex}")
    private int sex;}

2ConfigurationProperties

@Component
@ConfigurationProperties(prefix = "info")
public class TechUser {
    private String name;
    private int age;
    private int sex;}

 

读取指定文件

文件config/db-config.properties

db.username=root

db.password=123456

@PropertySource+@Value注解读取方式

@Component   这个不用get和set
@PropertySource(value = { "config/db-config.properties" })
public class DBConfig1 {
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;}

注意:@PropertySource不支持yml文件读取。

2.2@PropertySource+@ConfigurationProperties注解读取方式

@Component
@ConfigurationProperties(prefix = "db")
@PropertySource(value = { "config/db-config.properties" })
public class DBConfig2 {
    private String username;
    private String password;}

3.Environment读取方式

@Autowired private Environment env;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值