Spring注解与Java元注解小结

注解 Annotation

基于注解的开发,使得代码简洁,可读性高,简化的配置的同时也提高了开发的效率,尤其是SpringBoot的兴起,随着起步依赖和自动配置的完善,更是将基于注解的开发推到了新的高度。

元注解 meta-annotation

Java 5 定义了四个标准的元注解类型,用以提供对其它注解的功能说明。

位于java.lang.annotation包下,分别为:

  1. @Target

  2. @Retention

  3. @Documented

  4. @Inherited

以@Profile注解为例:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {

    /**
     * The set of profiles for which the annotated component should be registered.
     */
    String[] value();

}

@Target

说明注解所修饰的对象范围。

注解可用于:package、type(类、接口、枚举、注解)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(循环变量、catch参数)。

按照作用范围,个人给出的常用注解,按照作用范围排序,各类暂举一:

  @Configuration、@MapperScan、@RestController、@RequestMapping、@ResponseBody、@Autowired、@Resource、@Value、@PathVariable。。。

具体的取值范围为ElementType(enum)类型的数组,见源码:

@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();
}
  1. ElementType.Constructor : 描述构造器
  2. ElementType.Field : 描述域
  3. ElementType.LocalVariable : 描述局部变量
  4. ElementType.Method : 描述方法
  5. ElementType.Package : 描述包
  6. ElementType.Parameter : 描述参数
  7. ElementType.Type : 描述类、接口、enum、annotation

@Retention

定义注解的作用时期(源文件、字节码、运行期)。

用以 说明被描述的注解在什么范围内生效。

取值唯一,即具体的保存策略,RetentionPolicy(enum)之一。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

RetentionPolicy包括:

  1. SOURCE : 源文件有效
  2. CLASS : 字节码文件有效
  3. RUNTIME : 运行时有效

@Documented

标记注解,可被javadoc之类的工具文档化,即描述该类型的注解,应该作为被标注的程序成员的公共API,没有成员

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

@Inherited

标记注解,描述的注解是可被继承的。即如果一个类被@Inherited标记的注解所修饰,则该注解同样作用于这个类的子类。

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

原文地址:https://www.cnblogs.com/nyatom/p/9146451.html

时间: 2024-07-29 22:40:21

Spring注解与Java元注解小结的相关文章

java注解中的元注解

一:java注解中的元注解 四个元注解分别是:@Target,@Retention,@Documented,@Inherited , 再次强调下元注解是java API提供,是专门用来定义注解的注解,其作用分别如下: @Target 表示该注解用于什么地方,可能的值在枚举类 ElemenetType 中,包括: ElemenetType.CONSTRUCTOR----------------------------构造器声明 ElemenetType.FIELD ----------------

java 元注解

java元注解的作用是注解其他注解,java5.0定义了四个标准的元注解:@Target.@Retention.@Inherit.@Documented. 1)@Target:用于描述注解可以修饰的类型.其可选值为:(ElementType.TYPE) ANNOTATION_TYPE(注解类型声明) PACKAGE(包) TYPE(类.接口.枚举) METHOD(方法声明) FIELD(成员变量) LOCAL_VARIABLE(本地变量) CONSTRUCTOR(构造方法) 其代码如下: @Do

Java元注解

元注解的作用是负责注解其他注解.Java定义了4中标准的元注解类型,他们被用来提供对其他注解的说明. @target @Retention @Documented @Inherited 这些类型可以和他们所支持的类在Java.lang.annotation包中找到 每个元注解的作用: @target 修饰了annotation所修饰的对象范围,annotation可被用于package,type(类,接口,枚举,annotation),和类型成员(方法,构造方法,成员变量,枚举值).方法参数和本

java元注解详解

java中元注解有四个: @Retention @Target @Document @Inherited:  @Retention:注解的保留位置 @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含 @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得, @Retention(RetentionPolicy.RUNTIME

Spring完全基于Java和注解配置

要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法的返回值为一个Bean,相应于配置applicationContext.xml等spring的xml配置文件 配置启动类 继承WebApplicationInitializer并重新onStartup方法,加载配置类,相当于配置web.xml文件 1 public class WebAppInitC

Java元注解,简单案例

[注解] 程序中有 注释 和注解 * 注释:给开发人员. * 注解:给计算机看的. 注解使用:学习框架支持注解开发. [JDK提供的注解] @Override :描述方法的重写. @SuppressWarnings :压制警告. @Deprecated :标记过时. 自定义注解: 定义一个类:class 定义一个借口:interface 定义一个枚举:enum 定义一个注解:@interface 用法: @interface MyAnno1{ } 带有属性的注解: @interface MyAn

java元注解 @Target注解用法

@Target: @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages.types(类.接口.枚举.Annotation类型).类型成员(方法.构造方法.成员变量.枚举值).方法参数和本地变量(如循环变量.catch参数).在Annotation类型的声明中使用了target可更加明晰其修饰的目标. 作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方) 取值(ElementType)有 public enum ElementType

java元注解 @Retention注解使用

@Retention定义了该Annotation被保留的时间长短: 1.某些Annotation仅出现在源代码中,而被编译器丢弃: 2.另一些却被编译在class文件中,注解保留在class文件中,在加载到JVM虚拟机时丢弃,这是默认行为,所以没有用Retention注解的注解,都会采用这种策略 3.而另一些在class被装载时将被读取,注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解 作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围

java元注解 @Documented注解使用

@Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员. 源码 使用方法 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Column {