“Spring Boot 常见注解”的版本间的差异
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
=== @SpringBootApplication === | === @SpringBootApplication === | ||
− | spring-boot-autoconfigure-x.x.x.RELEASE.jar | + | 来自 spring-boot-autoconfigure-x.x.x.RELEASE.jar |
https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html | https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html | ||
第6行: | 第6行: | ||
表示一个配置类,声明一个或多个@Bean 方法,同时触发自动配置和组件扫描。 | 表示一个配置类,声明一个或多个@Bean 方法,同时触发自动配置和组件扫描。 | ||
− | 这是一个方便的注解,相当于声明@Configuration、@EnableAutoConfiguration 和@ComponentScan。 | + | 这是一个方便的注解,相当于声明@Configuration、@EnableAutoConfiguration 和 @ComponentScan。 |
+ | |||
=== @Configuration === | === @Configuration === | ||
+ | 来自 spring-context-x.x.x.jar | ||
+ | |||
+ | https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html | ||
+ | |||
+ | 指示一个类声明一个或多个@Bean 方法,并且可以由 Spring 容器处理以在运行时为这些 bean 生成 bean 定义和服务请求 | ||
=== @EnableAutoConfiguration === | === @EnableAutoConfiguration === | ||
+ | 来自 spring-boot-autoconfigure-x.x.x.RELEASE.jar | ||
+ | |||
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/api/index.html?org/springframework/boot/autoconfigure/condition/ConditionalOnClass.html | https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/api/index.html?org/springframework/boot/autoconfigure/condition/ConditionalOnClass.html | ||
启用 Spring Application Context 的自动配置,尝试猜测和配置您可能需要的 beans。 | 启用 Spring Application Context 的自动配置,尝试猜测和配置您可能需要的 beans。 | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === @ComponentScan === | ||
+ | 来自 spring-context-x.x.x.jar | ||
+ | |||
+ | https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html | ||
+ | |||
+ | 配置组件扫描指令以与 @Configuration 类一起使用。 提供与 Spring XML 的 <context:component-scan> 元素并行的支持。 | ||
+ | |||
=== @Bean === | === @Bean === | ||
− | spring-context-x.x.x.jar | + | 来自 spring-context-x.x.x.jar |
https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html | https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html |
2023年1月31日 (二) 02:49的版本
@SpringBootApplication
来自 spring-boot-autoconfigure-x.x.x.RELEASE.jar
表示一个配置类,声明一个或多个@Bean 方法,同时触发自动配置和组件扫描。
这是一个方便的注解,相当于声明@Configuration、@EnableAutoConfiguration 和 @ComponentScan。
@Configuration
来自 spring-context-x.x.x.jar
指示一个类声明一个或多个@Bean 方法,并且可以由 Spring 容器处理以在运行时为这些 bean 生成 bean 定义和服务请求
@EnableAutoConfiguration
来自 spring-boot-autoconfigure-x.x.x.RELEASE.jar
启用 Spring Application Context 的自动配置,尝试猜测和配置您可能需要的 beans。
@ComponentScan
来自 spring-context-x.x.x.jar
配置组件扫描指令以与 @Configuration 类一起使用。 提供与 Spring XML 的 <context:component-scan> 元素并行的支持。
@Bean
来自 spring-context-x.x.x.jar
https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html
@Bean 是一个方法级注释,是 XML <bean/> 元素的直接模拟。
@Conditional
spring-context-x.x.x.jar 中的注解
https://docs.spring.io/spring-framework/docs/5.3.1/javadoc-api/
表示组件只有在所有指定条件都匹配时才有资格注册。
可以是 bean 从定义到注册之前以编程方式确定的任何状态。
如果 @Configuration 类标有 @Conditional,则与该类关联的所有@Bean 方法、@Import 注释和@ComponentScan 注释都将受制于条件。
@ConditionalOnClass
只有当指定的类在类路径上时才匹配。
@ConditionalOnMissingBean
仅当指定的 bean 类和/或名称尚未包含在 BeanFactory 中时才匹配。
@ConditionalOnProperty
条件检查指定的属性是否具有特定值。
默认情况下,属性必须存在于环境中并且不等于 false。
havingValue() 和 matchIfMissing() 属性允许进一步定制。