SpringBoot常用注解分析

SpringBoot 常用注解分析

1.@Repository,@Component,@Service,@Controller

  • @Repository

    Indicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”.
    Teams implementing traditional Java EE patterns such as “Data Access Object” may also apply this stereotype to DAO classes, though care should be taken to understand the distinction between Data Access Object and DDD-style repositories before doing so. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.
    A class thus annotated is eligible for Spring DataAccessException translation when used in conjunction with a PersistenceExceptionTranslationPostProcessor. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tooling, aspects, etc.

    As of Spring 2.5, this annotation also serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning


    指示带注释的类是“存储库”,最初由Domain-Driven Design(Evans,2003)定义为“一种封装存储,检索和搜索行为的机制,该机制模仿对象的集合”。
    实现诸如“数据访问对象”之类的传统Java EE模式的团队也可以将这种构造型应用于DAO类,尽管在这样做之前应注意理解数据访问对象和DDD样式存储库之间的区别。此注释是通用的刻板印象,各个团队可以缩小其语义并适当使用。
    与PersistenceExceptionTranslationPostProcessor结合使用时,这样注释的类可以进行Spring DataAccessException转换。出于工具,方面等目的,已注释的类还明确了其在整个应用程序体系结构中的作用。

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

  • @Component

    Indicates that an annotated class is a “component”. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
    Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ’s @Aspect annotation


    指示带注释的类是“组件”。当使用基于注释的配置和类路径扫描时,此类将被视为自动检测的候选。
    其他类级别的注释也可以被认为是标识一个组件,通常是一种特殊的组件: @Repository批注或AspectJ的@Aspect批注

  • @Service

    Indicates that an annotated class is a “Service”, originally defined by Domain-Driven Design (Evans, 2003) as “an operation offered as an interface that stands alone in the model, with no encapsulated state.”
    May also indicate that a class is a “Business Service Facade” (in the Core J2EE patterns sense), or something similar. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.
    This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.


    指示带注释的类是“服务”,最初由域驱动设计(Evans,2003)定义为“作为接口提供的操作,在模型中是独立的,没有封装状态”。
    可能还表明某个类是“业务服务门面”(就核心J2EE模式而言)或类似的东西。此注释是通用的刻板印象,各个团队可以缩小其语义并适当使用。
    此批注用作@Component的特殊化,允许通过类路径扫描自动检测实现类。

  • @Controller

    Indicates that an annotated class is a “Controller” (e.g. a web controller).
    This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. It is typically used in combination with annotated handler methods based on the org.springframework.web.bind.annotation.RequestMapping annotation


    表示带注释的类是“控制器”(例如Web控制器)。
    此批注用作@Component的特殊化,允许通过类路径扫描自动检测实现类。它通常与基于org.springframework.web.bind.annotation.RequestMapping注释的带注释的处理程序方法结合使用。

  • Repository适用于操作持久层,Component是组件,有此注解时加入到spring容器中管理。Service则是用于业务逻辑层。Controller用于视图控制层

    • @Repository,@Service,@Controller都有Component的注解

2.@Autowired与@Resource区别

  • Autowired

    • package org.springframework.beans.factory.annotation; 是springframework中的注解

    • 类型装配依赖对象,默认情况下required为true 也就是装配对象必须存在,如果允许为null,则可以实现required为false

  • Resource

    • package javax.annotation; 是javax 中的注解

    • 名字装配依赖对象

  • 两者都可以用来装配Bean,只是装配的类型不同。

3.@Qualifier

自动装配时,此注释可以在字段或参数上用作候选bean的限定符。它也可以用于注释其他自定义注释,这些注释随后可以用作限定符。

  • 因为Autowired是按照类型装配依赖对象,所以就有如果存在多个实现类的Service,用@Qualifier就可以区分是哪个实现类的Bean
  • Resource 是按照名字来装配依赖对象,所以Resource有name字段来区分

4.@Target

注解的作用目标 表示该注解的作用域,如果超过了就会报错

package java.lang.annotation 是java.lang 包下的注解

@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包

5.@Retention

注解的保留位置

RetentionPolicy的取值有SOURCE,CLASS RUNTIME

SOURCE在源文件中有效(即源文件保留)
CLASS在class文件中有效(即class保留)
RUNTIME在运行时有效(即运行时保留)
SOURCE在源文件中有效,在编译时就会被丢

6.@Configuration

  • 告诉springboot这是一个配置类,类似于配置文件

  • 搭配@Bean 以方法名向容器中添加组件

  • 默认是单实例。

  • @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Configuration {
    
    	@AliasFor(annotation = Component.class)
    	String value() default "";
    	boolean proxyBeanMethods() default true;
    
    • proxyBeanMethods 是否 代理Bean方法

      true:在容器中保存有代理对象 全配置

      false:容器中没有代理对象,每次调用都产生一个新的对象 轻量级配置

7.@Conditional

条件装配

还有派生注解

  • @ConditionalOnBean

    容器中有某个组件时,生效

  • @ConditionalOnMissingBean

    容器中没有Bean时,生效

  • 就是一个判断条件

8.@ConfigurationProperties

这个注解可以直接与配置文件相关联

通过value() 或者prefix()定义前缀名,然后在yaml中可以使用


@Component
@ConfigurationProperties(prefix = "Cat" )
public class Cat{
 private String name;
 private String type;
}
Cat:
	name: jack
	type: 美短

使用@ConfigurationProperties必须把Cat类加入到@Component组件中才可以使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值