一文搞懂Spring注解

685 阅读7分钟

一、元注解

元注解:是用于修饰注解的注解,通常用在注解的定义上。一般用于指定某个注解生命周期以及作用目标等信息,为Java内置的注解,主要有如下几种:

  • @Target: 注解的作用目标
  • @Retention: 注解的生命周期
  • @Documented:注解是否应当被包含在 JavaDoc 文档中
  • @Inherited:是否允许子类继承该注解

1、@Target

用于指明被修饰的注解最终作用的目标是什么,是类还是方法等。

定义

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

从其中可以看到value是一个数组类型,可以看下ElementType的定义

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    //类、接口(包括注释类型)或枚举声明
    TYPE,
    /** Field declaration (includes enum constants) */
    //字段声明(包括枚举常量)
    FIELD,

    /** Method declaration */
    //方法声明上
    METHOD,

    /** Formal parameter declaration */
    //参数上
    PARAMETER,

    /** Constructor declaration */
    //构造器上
    CONSTRUCTOR,

    /** Local variable declaration */
    //本地变量上
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    //注解上
    ANNOTATION_TYPE,

    /** Package declaration */
    //包
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    //类型参数声明
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    //类型的使用
    TYPE_USE
}

使用

@Target(value = {ElementType.FIELD})

2、Retention

定义:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    //:当前注解编译期可见,不会写入 class 文件
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    //类加载阶段丢弃,会写入 class 文件
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

使用

@Retention(RetentionPolicy.RUNTIME)

3、@Documented

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

二、如何声明一个注解

这里我们可以参照下@Override

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

通过元注解我们可以知道以下信息

@Target(ElementType.METHOD):表面这个注解是用来修饰方法的

@Retention(RetentionPolicy.SOURCE):当前注解编译器可见,不会写入class文件的

三、自定义一个注解

先了解以下Class提供一些方法用于注解的

  • getAnnotation:返回指定的注解
  • isAnnotationPresent:判定当前元素是否被指定注解修饰
  • getAnnotations:返回所有的注解
  • getDeclaredAnnotation:返回本元素的指定注解
  • getDeclaredAnnotations:返回本元素的所有注解,不包含父类继承而来的
package com.trg.Annotation;


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotationTest {

    public static void main(String[] args) {
        Class<Bird> birdClass = Bird.class;
        //1、获取所有的字段信息
        Field[] declaredField = birdClass.getDeclaredFields();
        for (Field field:declaredField){
            if(field.isAnnotationPresent(ReqValue.class)){
                ReqValue annotation = field.getAnnotation(ReqValue.class);
                System.out.println("val1:"+annotation.val1());
                System.out.println("val2:"+annotation.val2());
            }
        }
        System.out.println("--------------获取所有的方法信息--------------------------");
        //2、获取所有的方法信息
        Method[] declaredMethods = birdClass.getDeclaredMethods();
        for (Method declaredMethod :declaredMethods ) {
            if(declaredMethod.isAnnotationPresent(ReqMapping.class)){
                ReqMapping annotation = declaredMethod.getAnnotation(ReqMapping.class);
                System.out.println("method:"+annotation.method());
                System.out.println("value:"+annotation.value());
            }
        }
        System.out.println("---------------获取类上的信息-------------------------");

        //3、获取类上的信息
        Annotation[] declaredAnnotations = birdClass.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            System.out.println("类注解值:"+ declaredAnnotation);
        }


        System.out.println("测试完毕");
    }
}

package com.trg.Annotation;

@ReqMapping(method = ReqMethod.GET,value = "Bird类")
public class Bird {
    @ReqValue(val1 = "xiaoniao")
    private String name;
    @ReqValue(val2 = "maque")
    private String weight;

    @ReqMapping(method = ReqMethod.GET,value = "GET")
    public void get(){

    }
    @ReqMapping(method = ReqMethod.POST,value = "POST")
    public void post(){

    }
}

package com.trg.Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author hp
 */
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface ReqMapping {
    ReqMethod[] method() default {};
    String value() default "";
}

package com.trg.Annotation;

public enum ReqMethod {
    GET,POST,PUT
}

package com.trg.Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author hp
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReqValue {
    String val1() default "";
    String val2() default "";
}

执行结果:

Connected to the target VM, address: '127.0.0.1:60939', transport: 'socket'
val1:xiaoniao
val2:
val1:
val2:maque
--------------获取所有的方法信息--------------------------
method:[Lcom.trg.Annotation.ReqMethod;@5afa04c
value:GET
method:[Lcom.trg.Annotation.ReqMethod;@6ea12c19
value:POST
---------------获取类上的信息-------------------------
类注解值:@com.trg.Annotation.ReqMapping(method=[GET], value=Bird类)
测试完毕
Disconnected from the target VM, address: '127.0.0.1:60939', transport: 'socket'

四、Spring中常见的注解

1、声明Bean的注解

  • @Component 组件,没有明确的角色
  • @Service 在业务逻辑层使用(service层)
  • @Repository 在数据访问层使用(dao层)
  • @Controller 在展现层使用,控制器的声明

2、注入bean的注解

  • @Autowired:由Spring提供,根据类型进行自动装配,如果组合@Qualifier使用将按名称进行装配
  • @Inject:由JSR-330提供使用时需要导入javax.inject.Inject;实现注入同样是根据类型进行自动装配,如果需要按名称进行装配,则需要配合@Named
  • @Resource:由JSR-250提供,使用需要导入javax.annotation,根据名称进行自动装配的,一般会指定一个name属性

3、功能注解

  • @Transactional声明事务
  • @Cacheable声明缓存

4、配置类相关注解

  • @Configuration 声明当前类为配置类
  • @Bean 注解在方法上,声明当前方法的返回值为一个bean
  • @ComponentScan 用于对Component进行扫描,
  • @WishlyConfiguration 为@Configuration与@ComponentScan的组合注解,可以替代这两个注解

5、切面(AOP)相关注解

  • @Aspect 声明一个切面
  • @After 在方法执行之后执行
  • @Before 在方法执行之前执行
  • @Around 在方法执行之前与之后都执行
  • @PointCut 声明切点

6、@Bean的属性支持

  • @scope

7、类生命周期注解

  • @PostConstruct 由JSR-250提供,在类的构造函数执行完之后执行,等价于xml配置文件中bean的initMethod
  • @PreDestory 由JSR-250提供,在Bean销毁之前执行

8、配置注入

@Value为属性注入值,支持如下方式的注入:

  • 普通字符@Value(“Michael Jackson”)
  • 操作系统属性@Value("#{systemProperties[‘os.name’]}")
  • 表达式结果@Value("#{ T(java.lang.Math).random() * 100 }")
  • 其它bean属性@Value("#{domeClass.name}")
  • 文件资源@Value(“classpath:com/ali/test.txt”)
  • 网站资源@Value(“juejin.cn/post/733200…”)
  • 配置文件属性@Value("${book.name}")

注入配置文件@PropertySource(“classpath:cn/ali/test.propertie”)

9、环境切换

  • @Profile 通过设定Environment的ActiveProfiles来设定当前context需要使用的配置环境
  • @Conditional根据代码中设置的条件装载不同的bean,包括一系列的注解
    • @ConditionalOnBean存在bean
    • @ConditionalOnMissingBean不存在bean
    • @ConditionalOnClass存在某个类
    • @ConditionalOnMissingClass不存在某个类
    • @ConditionalOnProperty(prefix = “syj”, name = “algorithm”, havingValue = “token”)当存在配置文件中以syj为前缀的属性,属性名称为algorithm,然后它的值为token时才会实例化一个类。
    • @ConditionalOnProperty(prefix = “syj”, name = “algorithm”, havingValue = “counter”, matchIfMissing = true)如果所有的都不满足的话就选择counter为默认实现
    • @ConditionalOnJava如果是Java应用
    • @ConditionalOnWebApplication如果是Web应用

10、异步相关

  • @Async 在实际执行的bean方法使用该注解来申明其是一个异步任务

11、定时任务相关

  • @Scheduled 来申明这是一个任务,包括cron,fixDelay,fixRate等类型

12、开启某些功能

  • @EnableAspectJAutoProxy 开启对AspectJ自动代理的支持
  • @EnableAsync 开启异步方法的支持
  • @EnableScheduling 开启计划任务的支持
  • @EnableWebMvc 开启Web MVC的配置支持
  • @EnableConfigurationProperties 开启对@ConfigurationProperties注解配置Bean的支持
  • @EnableJpaRepositories 开启对SpringData JPA Repository的支持
  • @EnableTransactionManagement 开启注解式事务的支持
  • @EnableCaching 开启注解式的缓存支持

13、测试相关注解

  • @RunWith Spring中通常用于对JUnit的支持
  • @ContextConfiguration 用来加载配置ApplicationContext

14、SpringMVC部分

  • @RequestMapping 用于映射Web请求,包括访问路径和参数
  • ResponseBody 支持将返回值放在response内,而不是一个页面
  • @PathVariable 用于接收路径参数
  • @RestController 该注解为一个组合注解,相当于@Controller和@ResponseBody的组合,注解在类上,意味着,该Controller的所有方法都默认加上了@ResponseBody
  • @ControllerAdvice 通过该注解,我们可以将对于控制器的全局配置放置在同一个位置,注解了@Controller的类的方法可使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上, 这对所有注解了 @RequestMapping的控制器内的方法有效。
  • @ExceptionHandler 用于全局处理控制器里的异常
  • @InitBinder 用来设置WebDataBinder,WebDataBinder用来自动绑定前台请求参数到Model中。
  • @ModelAttribute 本来的作用是绑定键值对到Model里,在@ControllerAdvice中是让全局的@RequestMapping都能获得在此处设置的键值对
  • @InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效。在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题

15、SpringBoot相关

  • @EnableAutoConfiguration自动载入应用程序所需的所有Bean。该注解组合了@Import注解,@Import注解导入了EnableAutoCofigurationImportSelector类,它使用SpringFactoriesLoader.loaderFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包。而spring.factories里声明了有哪些自动配置 ​+ @SpingBootApplication SpringBoot的核心注解,主要目的是开启自动配置。它也是一个组合注解,主要组合了@Configurer,@EnableAutoConfiguration(核心)和@ComponentScan。可以通过@SpringBootApplication(exclude={想要关闭的自动配置的类名.class})来关闭特定的自动配置
  • @ImportResource加载xml配置的
  • @AutoConfigureAfter在指定的自动配置类之后再配置

参考:www.cnblogs.com/zhixiang-or…

微信关注【唐小码】,有更多精彩内容哦,更新频率频繁,经常更新面试题目