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;