@ConfigurationProperties注入属性
概述
获取到了自定义属性还是官方的属性,都通过==@Value(“${key}”)==来获取。除了这种方式外,springboot还提供一种 @ConfigurationProperties
属性注入的机制。这也是如下配置的原理
# 环境激活
spring:
profiles:
active: dev
分析
@Value(“${key}”) 存在的问题:不具有面向对象的特征。比如:
#自定义属性
ksd:
weixin:
appid: 456453sdfsd52342
mcid: 48878787
callbackurl: https://www.baidu.com
apisecret: SDFLKSDJFKLSJKLJ23423423
@ConfigurationProperties :一种具有面向对象特种的属性注入的方式,如何做到呢?如下:
定义一个属性配置类:
package com.example.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "weixin")
@EnableConfigurationProperties(WeixinPayProperties.class)
@Configuration
public class WeixinPayProperties {
//#自定义属性
//weixin:
// appId: 456453sdfsd52342
// mcId: 48878787
// callbackUrl: https://www.baidu.co
// apiScret: SDFLKSDJFKLSJKLJ23423423
private String appId;
private String mcId;
private String callbackUrl;
private String apiSecret;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getMcId() {
return mcId;
}
public void setMcId(String mcId) {
this.mcId = mcId;
}
public String getCallbackUrl() {
return callbackUrl;
}
public void setCallbackUrl(String callbackUrl) {
this.callbackUrl = callbackUrl;
}
public String getApiSecret() {
return apiSecret;
}
public void setApiSecret(String apiSecret) {
this.apiSecret = apiSecret;
}
}
注册配置类到springboot中:
springboot加载这个属性配置类以及完成属性注入:
@EnableConfigurationProperties(WeixinPayProperties.class)
@Configuration
如何属性配置类:
package com.example.service;
import com.example.config.WeixinPayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class WeiXinPayService {
@Autowired
private WeixinPayProperties weixinPayProperties;
// @Value("${weixin.appId}")9
// private String appId;
// @Value("${weixin.mcId}")
// private String mcId;
// @Value("${weixin.callbackUrl}")
// private String callbackUrl;
// @Value("${weixin.apiSecret}")
// private String apiSecret;
public void testValue(){
System.out.println(weixinPayProperties.getAppId());
System.out.println(weixinPayProperties.getMcId());
System.out.println(weixinPayProperties.getCallbackUrl());
System.out.println(weixinPayProperties.getApiSecret());
}
}
打印结果:
怎么选择@Value和 @ConfigurationProperties?
作为一个开发者,不应该纠结到底使用哪种好哪种坏,因为两者实现和最终的目的都是一样的。
- 在开发中如果属性很少的情况可以考虑使用
@Value
- 如果自定义属性的属性过多或者相关性很强的话,建议使用
@ConfigurationProperties
关于自定义属性配置类的自动提示的问题
参考官网:https://docs.spring.io/spring-boot/docs/2.4.7/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor
在项目的pom.xml中配置:
<!--把项目中的springboot自定义属性配置类生成一个元素数据文件,这个文件可以生成以后
在未来的配置文件中,我们就达到和官方一致效果,可以自动提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
重新编译工程(核心步骤):
注意:一定要把项目打开的配置文件全部关闭。
然后重新编译项目
手动编译:
mvn clean compile
用工具编译: