一、场景复现
application.yaml
spring:application:name: config-testprofiles:active: devconfig:config-01:name: zhansancode: 001config-02:name: lisicode: 002
导入配置类
@Configuration
@Import(ImportConfig.class)
public class Config {@Bean@ConfigurationProperties(prefix = "config.config-a")public ConfigInfo oneConfig() {return new ConfigInfo();}@Beanpublic String oneName() {ConfigInfo configInfo = this.oneConfig();return configInfo.getName();}}
Import配置类
public class ImportConfig {@Bean@ConfigurationProperties(prefix = "config.config-b")public ConfigInfo twoConfig() {return new ConfigInfo();}@Beanpublic String twoName() {ConfigInfo configInfo = this.twoConfig();return configInfo.getName();}@Beanpublic String twoName1(@Qualifier("twoConfig") ConfigInfo configInfo) {return configInfo.getName();}
}
运行结果
oneName为zhansan
twoName为null
twoName为lisi
二、分析原因
由于@Configuration标注的类代理,自动增强拦截功能。@Import的类没有被@Configuration修饰,所有没有增强方法注入