Spring 引导自动配置 类 应该使用 @Configuration 注释吗?
Should Spring Boot auto-configuration classes use the @Configuration annotation?
根据我的测试,我发现 Spring 启动自动配置 类 有或没有 @Configuration
注释时没有明显区别 - 如果它们已配置在 spring.factories
中,无论 @Configuration
注释如何,它们都会正确加载。
但是,似乎每个自定义自动配置示例和演示都使用了 @Configuration
注释。所有这些示例都使用 @Configuration
是有原因的(还是只是约定俗成)?不使用 @Configuration
有什么影响吗?
在自动配置中使用 @Configuration
的一些示例 类:
- https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration.condition-annotations.class-conditions
- https://www.baeldung.com/spring-boot-custom-auto-configuration#creating-a-custom-auto-configuration
- https://www.baeldung.com/spring-boot-custom-starter#1-auto-configuration-classes
- https://www.javadevjournal.com/spring-boot/spring-boot-custom-starter/
- https://billykorando.com/2019/12/30/building-a-custom-spring-boot-starter/
- http://www.masterspringboot.com/getting-started-with-spring-boot/spring-boot-quickstarts/how-to-build-a-custom-spring-boot-starter-in-no-time/
- https://github.com/snicoll/spring-boot-master-auto-configuration/blob/main/hornetq-spring-boot-autoconfigure/src/main/java/hornetq/autoconfigure/HornetQAutoConfiguration.java
是的,他们应该是。文档指出 an auto-configuration class should be annotated with @Configuration
:
Under the hood, auto-configuration is implemented with standard @Configuration
classes. Additional @Conditional
annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use @ConditionalOnClass
and @ConditionalOnMissingBean
annotations.
如果您不使用 @Configuration
注释它们,您将依赖 Spring 框架的 “lite” @Bean mode。这改变了 auto-configuration 类 的行为。由于实施不符合记录的要求,因此不能保证将来能正常工作。
根据我的测试,我发现 Spring 启动自动配置 类 有或没有 @Configuration
注释时没有明显区别 - 如果它们已配置在 spring.factories
中,无论 @Configuration
注释如何,它们都会正确加载。
但是,似乎每个自定义自动配置示例和演示都使用了 @Configuration
注释。所有这些示例都使用 @Configuration
是有原因的(还是只是约定俗成)?不使用 @Configuration
有什么影响吗?
在自动配置中使用 @Configuration
的一些示例 类:
- https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration.condition-annotations.class-conditions
- https://www.baeldung.com/spring-boot-custom-auto-configuration#creating-a-custom-auto-configuration
- https://www.baeldung.com/spring-boot-custom-starter#1-auto-configuration-classes
- https://www.javadevjournal.com/spring-boot/spring-boot-custom-starter/
- https://billykorando.com/2019/12/30/building-a-custom-spring-boot-starter/
- http://www.masterspringboot.com/getting-started-with-spring-boot/spring-boot-quickstarts/how-to-build-a-custom-spring-boot-starter-in-no-time/
- https://github.com/snicoll/spring-boot-master-auto-configuration/blob/main/hornetq-spring-boot-autoconfigure/src/main/java/hornetq/autoconfigure/HornetQAutoConfiguration.java
是的,他们应该是。文档指出 an auto-configuration class should be annotated with @Configuration
:
Under the hood, auto-configuration is implemented with standard
@Configuration
classes. Additional@Conditional
annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use@ConditionalOnClass
and@ConditionalOnMissingBean
annotations.
如果您不使用 @Configuration
注释它们,您将依赖 Spring 框架的 “lite” @Bean mode。这改变了 auto-configuration 类 的行为。由于实施不符合记录的要求,因此不能保证将来能正常工作。