Spring 注解

注解

0.注解定义

1.Annotation即注解(也被翻译为注释)

  • 所有的注解会自动继承java.lang.annotation.Annotation(这个接口是一个常规接口,并非一个注解接口),并不能继承别的类或是接口
  • 当一个注解的内部只有一个,且字段为value时,在使用的时候可以省略

2.自定义注解的使用:代码仓库见下

https://gitee.com/gqxm/gq-practice-glodon.git

  • 自定义注解的简单使用:主要对ElementType.TYPE,METHOD,FIELD,PARAMETER等进行了测试和使用
  • 自定义注解的简单使用:自定义NeedLogin注解,并且通过拦截器对其进行测试

1.jdk标准注解

1.元注解

JDK在java.lang.annotation包下提供了6个Meta注解(元注解),其中5个用于修饰其他的注解定义。

注解 作用
@Target 用于指定被修饰的注解能用于修饰哪些程序单元(ElementType)。一个没有被此注解限制的注解可以应用于任何项上。
@Retention 用于指定被修饰的注解可以保留多长时间。当未使用该注解修饰时,默认值相当于是RetentionPolicy.CLASS
@Documented 修饰注解类表明其将被javadoc工具提取成文档
@Inherited 修饰作用于类的注解,指定被修饰的注解具有继承性:即该注解修饰的类的子类也将同样被该注解修饰
@Repeatable 指明被修饰的这个注解可以在同一个元素上应用多次
@Native 修饰成员变量,表示这个变量可以被本地代码引用,常常被代码生成工具使用(此注解不常用,了解即可)

将一个注解应用到它自身上是合法的。例如,@Documented注解被自身为注解为@Documented。因此,针对注解的Javadoc文件可以表明它们是否可被归档。

1.@Target

ElementType

关于程序单元的枚举类型ElementType

  • TYPE:类、接口、注解、枚举
  • FIELD:字段(含枚举值)
  • METHOD:方法
  • PARAMETER:形参(Formal parameter declaration)
  • CONSTRUCTOR:构造方法
  • LOCAL_VARIABLE:局部变量
  • ANNOTATION_TYPE:注解类型
  • PACKAGE:包
  • TYPE_PARAMETER:类型参数
  • TYPE_USE:类型用法
2.@Retention

RetentionPolicy

用于@Retention注解的保留策略:

保留规则 描述
SOURCE 不包括在类文件中的注解
CLASS 包括在类文件中的注解,但是虚拟机不需要将它们载入
RUNTIME 包括在类文件中的注解,并由虚拟机载入。通过反射API可获得它们
3.@Inerited 详解

如果一个类具有继承注解(即被@Inherited修饰的注解),那么它的所有子类都自动具有相同的注解。

  • 假设定义了一个继承注解@Persistent来指定一个类的对象可以存储到数据库中,那么该持久类的子类就会自动被注解为是持久性的。

    @Inherited
    @interface Persistent {
         
         
    }
    
    @Persistent
    class Employee {
         
         
    }
    
    class Manager extends Employee {
         
          // also @Persistent
    }
    
4.@Repeatable 详解

源码:

package java.lang.annotation;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
   
   
    /**
     * Indicates the <em>containing annotation type</em> for the
     * repeatable annotation type.
     * @return the containing annotation type
     */
    Class<? extends Annotation> value();
}

对于Java SE 8来说,将同种类型的注解多次应用于某一项是合法的。为了向后兼容,可重复注解的实现者需要提供一个容器注解,它可以将这些重复注解存储到一个数组中

例子:

@Repeatable(TestRepeatables.class)
public @interface TestRepeatable {
   
   
    String content() default "请添加描述";
}

数组,用来存储

@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestRepeatables {
   
   
    TestRepeatable[] value();
}
@TestRepeatable(content = "张三")
@TestRepeatable(content = "李四")
@TestRepeatable(content = "王五")
public class Student {
   
   
}
@Slf4j
public class StudentTest {
   
   
    @Test
    public void testRepeatable() {
   
   
        Annotation[] annotations = Student.class.getAnnotations();
        for (Annotation annotation : annotations) {
   
   
            // 输出注解的名称
            log.info(annotation.annotationType().getName());
            TestRepeatables testRepeatables = (TestRepeatables) annotation;
            // 输出注解的内容
            for (TestRepeatable a : testRepeatables.value()) {
   
   
                System.out.println(a.content());
            }
        }
    }

}

2.用于编译的注解

6个基本的注解如下,除了@Generated位于javax.annotation包,其它均定义在java.lang包下:

注解 修饰的程序单元 作用
@Deprecated 全部 表示某个程序元素(类、方法等)已过时,编译器会发出警告
@SuppressWarnings 除了包和注解之外的所有情况 取消显示指定的编译器警告
@SafeVarargs 方法、构造器 断言varargs参数可安全使用,抑制堆污染警告
@Override 方法 强制一个子类的方法必须是覆盖父类方法的方法
@FunctionalInterface 接口 指定某个接口必须是函数式接口
@Generated 全部 供代码生成工具来使用,任何生成的源代码都可以被注解,从而与程序员提供的代码区分开

3.用于管理资源的注解

注解 修饰的程序单元 作用
@PostConstruct @PreDestroy 方法 被标记的方法应该在构造之后/移除之前立即被调用
@Resource 类、接口、方法、域 在类/接口上:标记为在其他地方要用到的资源 在方法/域上:为“注入”而标记
@Resources 类、接口 一个资源数组

2.Spring常用注解

基本注解 解释
@Component 使用在类上,用于实例化Bean 代替xml的标签
@Controller @Component的衍生注解:使用在Web层上,用于实例化Bean
@Service @Component的衍生注解:使用在service层类上,用于实例化Bean
@Repository @Component的衍生注解:使用在dao层类上,用于实例化Bean
@Autowired 通常使用在字段上,用于根据Bean的类型进行依赖注入
@Qualifier 结合@Autowired一起使用用于同时根据类型和名称进行依赖注入
@Resource 相当于@Autowired+@Qualifier,按照名称进行注入
@Value 注入普通属性
@Scope 标注Bean的作用范围
@PostConstruct 使用在方法上,标注该方法是Bean的初始化方法
@PreDestroy 使用在方法上,标注该方法是Bean的销毁方法
@Configuration 用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定Spring在初始化容器时要扫描的包,作用和在Spring的xml配置文件中的<context:component-scan base-package="com.itheima"/>一样
@Bean 使用在方法上,表示将该方法的返回值存储到Spring容器中,并可赋予指定名称
@PropertySource 用于加载properties文件中的配置
@Import 用于导入其他配置类
@AliasFor 重新声明元注解中的属性

1.@Required

The @Required annotation and RequiredAnnotationBeanPostProcessor are formally deprecated as of Spring Framework 5.1.

/** @deprecated */
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target({
    
    ElementType.METHOD})
public @interface Required {
    
    
}

The @Required annotation applies to bean property setter methods, as in the following example:

public class SimpleMovieLister {
   
   

    private MovieFinder movieFinder;

    @Required
    public void setMovieFinder(MovieFinder movieFinder) {
   
   
        this.movieFinder = movieFinder;
    }

    // ...
}

This annotation indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring.

2.@Autowired

很多情况下,JSR 330提供的@Inject注解可以替换@Autowired的作用。

@Autowired根据类型注入Bean,对应类型的Bean需要是单例的。

@Target({
   
   ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值