Spring Boot 常见注解

来自姬鸿昌的知识库
跳到导航 跳到搜索

spring-boot-autoconfigure-x.x.x.RELEASE.jar

@SpringBootApplication

来自 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

表示一个配置类,声明一个或多个@Bean 方法,同时触发自动配置和组件扫描。

这是一个方便的注解,相当于声明@Configuration、@EnableAutoConfiguration 和 @ComponentScan。


@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

启用 Spring Application Context 的自动配置,尝试猜测和配置您可能需要的 beans。


@ConditionalOnClass

来自 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

只有当指定的类在类路径上时才匹配。


@ConditionalOnMissingBean

来自 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/ConditionalOnMissingBean.html

仅当指定的 bean 类和/或名称尚未包含在 BeanFactory 中时才匹配。


@ConditionalOnProperty

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/api/index.html?org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html

条件检查指定的属性是否具有特定值。

默认情况下,属性必须存在于环境中并且不等于 false。

havingValue() 和 matchIfMissing() 属性允许进一步定制。


spring-context-x.x.x.RELEASE.jar

@Configuration

来自 spring-context-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

指示一个类声明一个或多个@Bean 方法,并且可以由 Spring 容器处理以在运行时为这些 bean 生成 bean 定义和服务请求


@ComponentScan

来自 spring-context-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html

配置组件扫描指令以与 @Configuration 类一起使用。 提供与 Spring XML 的 <context:component-scan> 元素并行的支持。


@Component

来自 spring-context-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Component.html

指示带注释的类是“组件”。 当使用基于注解的配置和类路径扫描时,这些类被认为是自动检测的候选者。


@Service

来自 spring-context-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Service.html

表示带注释的类是“服务”,最初由领域驱动设计(Evans,2003)定义为“作为模型中独立的接口提供的操作,没有封装状态”。

也可能表明一个类是“业务服务外观”(在核心 J2EE 模式意义上),或类似的东西。

此注释用作 @Component 的特化,允许通过类路径扫描自动检测实现类。


@Bean

来自 spring-context-x.x.x.RELEASE.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.RELEASE.jar

https://docs.spring.io/spring-framework/docs/5.3.1/javadoc-api/

表示组件只有在所有指定条件都匹配时才有资格注册。

可以是 bean 从定义到注册之前以编程方式确定的任何状态。

如果 @Configuration 类标有 @Conditional,则与该类关联的所有@Bean 方法、@Import 注释和@ComponentScan 注释都将受制于条件。


@Controller

来自 spring-context-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Controller.html

指示带注释的类是“控制器”(例如 Web 控制器)。

此注释用作 @Component 的特化,允许通过类路径扫描自动检测实现类。

它通常与基于 RequestMapping 注释的注释处理程序方法结合使用。


spring-web-x.x.x.RELEASE.jar

@ResponseBody

来自 spring-web-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html

指示方法返回值应绑定到 Web 响应正文的注释。

支持带注释的处理程序方法。

从 4.0 版开始,这个注释也可以添加到类型级别,在这种情况下它是继承的,不需要添加到方法级别。


@RequestMapping

来自 spring-web-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html

用于将 Web 请求映射到特定处理程序类和/或处理程序方法的注释。


@RestController

来自 spring-web-x.x.x.RELEASE.jar

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html

一个方便的注解,它本身用@Controller 和@ResponseBody 进行注解。

带有此注释的类型被视为控制器,其中 @RequestMapping 方法默认采用 @ResponseBody 语义。


spring-beans-x.x.x.jar

@Qualifier

指定注入的 bean 的名称