最近写了一个注解,用的默认的Retention,也就是CLASS,里面import了一个bean,发现将这个注解加在springboot的启动类上的时候,import的bean无法实例化,如果是把这个注解加在普通@Configuration配置类的时候是可以实例化import的bean的。如果把这个注解的RetentionPolicy 改为 runtime ,加在启动类的时候也是可以实例化bean的!弄了一下午没搞明白原理!求助大神们!
这个是我写的注解,LogConfig为一个bean
@Import({LogConfig.class})
public @interface EnableCPSPLogConfig {
}
@Configuration
@EnableConfigurationProperties
@RefreshScope
@ConfigurationProperties(
prefix = "cpsplog"
)
public class LogConfig {
private List<String> include;
private List<String> exclude;
public LogConfig() {
}
}
这种情况是可以正常使用的:
@Configuration
@EnableCPSPLogConfig
public class WebConfig {
}
这种情况下必须要把RetentionPolicy 改为 runtime也是可以使用的
@SpringBootApplication
@EnableCPSPLogConfig
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}