使用springboot获取以List类型为主的配置项 (包含list嵌套的使用)
1.在application.yml 主配置文件中配置以smtpmail开头的配置项
#自定义 发送邮件的配置
smtpmail:
server: smtpmail #定义邮件服务的名字
# 发送邮件失败的原因分析
faileRepate:
errorKeyword:
- DT:SPM
- Limit
errorReason:
- 被网易邮箱认定为垃圾邮件
- 发送频率超过规定限制,请稍候再试
# 邮件主体中图片资源包含以下域名的才需要上传到邮件服务器
sourceUploadDomains:
- crm.xxxxxxxx.cn
- crm2.xxxxxxxx.cn
- crm3.xxxxxxxx.cn
- 讲解两种常用的获取配置方式
(方法1)创建 SmtpmailConfig.java 配置类用于获取配置
@ConfigurationProperties 注解作用在类上
package com.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Data
@ConfigurationProperties(prefix="smtpmail")// 该配置类主要用于读取 application.yml 中 smtpmail的配置
public class SmtpmailConfig{
// 获取 smtpmail 中的 sourceUploadDomains
private List<Object> sourceUploadDomains;
private String server;
@Bean
@ConfigurationProperties(prefix = "smtpmail.failerepate")
public FaileRepate FaileRepate(){
return new FaileRepate();
}
public static class FaileRepate{
List<Object> errorKeyword = new ArrayList<>();
List<Object> errorReason = new ArrayList<>();
// 获取 smtpmail.faileRepate.errorKeyword
public List<Object> getErrorKeyword(){
return this.errorKeyword;
}
// 获取 smtpmail.faileRepate.errorReason
public List<Object> getErrorReason(){
return this.errorReason;
}
}
}
(方法2)创建 ApplicationConfig.java 配置类用于获取application.yml所有配置
@ConfigurationProperties 注解作用在方法上
package com.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Data
/**
* 定义 application.yml 主配置类获取所有的配置
* @ConfigurationProperties 注解作用在方法上
*/
public class ApplicationConfig
{
@Bean
@ConfigurationProperties(prefix = "smtpmail")
public SourceUploadDomains SourceUploadDomains(){
return new SourceUploadDomains();
}
public static class SourceUploadDomains{
List<Object> sourceUploadDomains = new ArrayList<>();
public List<Object> getSourceUploadDomains(){
return this.sourceUploadDomains;
}
}
@Bean
@ConfigurationProperties(prefix = "smtpmail.failerepate")
public FaileRepate FaileRepate(){
return new FaileRepate();
}
public static class FaileRepate{
List<Object> errorKeyword = new ArrayList<>();
List<Object> errorReason = new ArrayList<>();
// 获取 smtpmail.faileRepate.errorKeyword
public List<Object> getErrorKeyword(){
return this.errorKeyword;
}
// 获取 smtpmail.faileRepate.errorReason
public List<Object> getErrorReason(){
return this.errorReason;
}
}
// ...todo 继续编写获取其他 application.yml 配置
}
3.使用单元测试获取属性
@Autowired
private SmtpmailConfig.FaileRepate faileRepate;
@Autowired
private SmtpmailConfig smtpmailConfig;
@Test
void test1() {
log.error(smtpmailConfig.getSourceUploadDomains().toString());
log.error(faileRepate.getErrorKeyword().toString());
log.error(faileRepate.getErrorReason().toString());
log.error(smtpmailConfig.getServer());
}