Springboot获取yml文件配置

本文介绍了在Springboot中如何使用@Value和@ConfigurationProperties来获取yml配置文件的信息。详细步骤包括@Value的直接获取,以及@ConfigurationProperties的配置,包括pom.xml的依赖引入,启动类注解,yml配置以及Java类的编写。同时,文章还提到了在使用@ConfigurationProperties时可能遇到的问题,如非静态set方法的要求,get/set方法的添加,以及启用注解的必要性,并讨论了配置的热加载机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、@Value 获取信息 ,获取

xxx.yml

....
productCompares:
  url: https://localhost:8080/manage/#/usercompare
...

xxxx.java

public class ProductInfo {
    @Value("${productCompares.url}")
    private String productCompares;
    
}

2、@ConfigurationProperties读取配置文件属性

a、pom.xml引入包

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

b、启动类需加入注解@EnableConfigurationProperties

c、xxx.yml配置

#小写 对应pfix
wechat:
  corpId: asdfasdfasdf
  toKen: 123123123
  encodingAesKey: sdfasdfasvvxzUOj
  customerContactSecret: AEMWm4ASDFASDF8352SWERFFDASDFmafHs
  addressBookSecret: ASDFW23R2Fqwe2334t45234534HDjffXwQk6oSLJG0eo
  qwApiUrl: https://qyapi.weixin.qq.com/cgi-bin

d、调用java类


import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 功能描述:企业微信配置信息
 *
 */
@Component
@ConfigurationProperties(prefix="wechat")
@ToString
@Data
public class QiWeiConfigInfo {

    /**企业ID*/
    public static String corpId;

    /**token*/
    public static String toKen;

    /**encodingAESKey*/
    public static String encodingAesKey;

    /**客户联系密钥*/
    public static String customerContactSecret;

    /**通讯录密钥*/
    public static String addressBookSecret;

    /**企业微信链接*/
    public static String qwApiUrl;

    public synchronized String getCorpId() {
        return corpId;
    }

    public synchronized void setCorpId(String corpId) {
        QiWeiConfigInfo.corpId = corpId;
    }

    public synchronized String getToKen() {
        return toKen;
    }

    public synchronized void setToKen(String toKen) {
        QiWeiConfigInfo.toKen = toKen;
    }

    public synchronized String getEncodingAesKey() {
        return encodingAesKey;
    }

    public synchronized void setEncodingAesKey(String encodingAesKey) {
        QiWeiConfigInfo.encodingAesKey = encodingAesKey;
    }

    public synchronized String getCustomerContactSecret() {
        return customerContactSecret;
    }

    public synchronized void setCustomerContactSecret(String customerContactSecret) {
        QiWeiConfigInfo.customerContactSecret = customerContactSecret;
    }

    public synchronized String getAddressBookSecret() {
        return addressBookSecret;
    }

    public synchronized void setAddressBookSecret(String addressBookSecret) {
        QiWeiConfigInfo.addressBookSecret = addressBookSecret;
    }

    public synchronized String getQwApiUrl() {
        return qwApiUrl;
    }

    public synchronized void setQwApiUrl(String qwApiUrl) {
        QiWeiConfigInfo.qwApiUrl = qwApiUrl;
    }
}

3、遇到的问题

遇到的问题@ConfigurationProperties未生效

1、@ConfigurationProperties只会调用 非静态的set方法

2、加入get set方法,@Setter @Getter

3、启动类加入注解:@EnableConfigurationProperties

4、热加载(动态加载)

//还需要对所在的类加上下面的注解才能动态配置
@RefreshScope
......
@Value("${sendMailConfig.mailBox}")
String mailBox;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值