@ConfigurationProperties必须和@Component联合使用么?
来源:5-2 【简单好用的SpringBoot】SpringBoot 常用功能特性介绍

windforce1989
2019-02-13
老师好,
我注意到这节内容中实例项目里MySQLConfig类上同时用到了@ConfigurationProperties和@Component.
我想问这里@Component可以用@Configuration注解代替吗?
写回答
1回答
-
张勤一
2019-02-13
同学你好:
@Configuration 注解本质上还是 @Component,所以,是可以替换的。且配置类用 @Configuration 标注也会更加准确,非常感谢你的建议。
可以参考 @Configuration 的源码:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { /** * Explicitly specify the name of the Spring bean definition associated * with this Configuration class. If left unspecified (the common case), * a bean name will be automatically generated. * <p>The custom name applies only if the Configuration class is picked up via * component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}. * If the Configuration class is registered as a traditional XML bean definition, * the name/id of the bean element will take precedence. * @return the suggested component name, if any (or empty String otherwise) * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator */ @AliasFor(annotation = Component.class) String value() default ""; }
可以看到,Configuration 实际是标注了 @Component 的。
欢迎来 QQ 群随时交流、讨论,也非常感谢同学的支持!
10
相似问题