spring boot 读取配置文件问题,不能使用@Autowired

来源:互联网 发布:照片尺寸修改软件 编辑:程序博客网 时间:2024/06/08 13:09

1.属性文件类

@ConfigurationProperties(prefix = "xxx.elklog")public class ElkLogConfig {    private String appName;    private String instanceID;    private String pointcutCtrl;    private String pointcutDao;    private String pointcutService;

2.配置类

@Configuration@EnableConfigurationProperties(ElkLogConfig.class)public class ElkLogConfiguration {    private ElkLogConfig elkLogConfig;    public ElkLogConfiguration(ElkLogConfig elkLogConfig) {        this.elkLogConfig = elkLogConfig;    }

application.yml

xxx.elklog: appName: app-demo instanceID: 1002

解决使用

@Autowired 

private ElkLogConfig elkLogConfig;

elkLogConfig这个变量为空的问题。使用构造函数可以解决。