问题描述
xxljob自动注册失败, Ip地址为空, 查看日志发现XxlJobConfig中@Bean未加载
SpringBoot版本: 2.0.2.RELEASE
解决方案
使用以下两种配置方法均可
@Data
@Configuration
@ConfigurationProperties(prefix = "xxl.job")
==
@Data
@ConfigurationProperties(prefix = "xxl.job") --- ①
+
@EnableConfigurationProperties(XxlJobConfig.class) --- 需和 ① 配置在同一个类
原因分析
在之前的提交中, 在ABCDEFGConfig
中使用了@EnableConfigurationProperties(XxlJobConfig.class), 后边业务变更删除了ABCDEFGConfig
, 造成问题发生
查漏补缺
- @Configuration 包含 @Component 注解, 会自动注入进 IOC 容器
- @ConfigurationProperties 单独使用不会加载进 IOC 容器
- @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入
注意
@ConfigurationProperties(prefix = "a.b")
public class AConfig {
private String c;
}
@Configuration
@EnableConfigurationProperties(AConfig.class)
public class BConfig {
}
配置不生效!!!
参考文章:
关与 @EnableConfigurationProperties 注解
spring boot 使用@ConfigurationProperties