Java注解的编写

Java注解的编写

我觉得这篇文章写的挺好的:https://blog.csdn.net/qq1404510094/article/details/80577555
原理可以看这篇文章:https://blog.csdn.net/qq_20009015/article/details/106038023

注解是什么:
注解就是用来描述包、类、成员变量、方法或者参数具体应该是什么样子的,官方一点的,注解是一系列元数据,它提供数据用来解释程序代码,但是注解并非是所解释的代码本身的一部分。注解对于代码的运行效果没有直接影响。。
注解本身也是一个类(Java里一切都是对象)。配置过Spring的肯定很清楚,Spring中的Bean需要在xml里面声明,比如说一个Bean他的构造方法是啥,他依赖哪些其他bean,初始化变量值是多少,这些都是描述性的数据告知Spring按照这个描述去创建这个Bean。
举个例子:

	<bean id="Kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells"></property>
        <property name="age" value="37"></property>
        <property name="instrument" ref="saxphone"></property> 
        <property name="instrument" ref="piano"></property> 
        <property name="instrument">
            <bean class="com.springinaction.springidol.piano"></bean>
        </property>
    </bean>

这些对类和变量等的描述实际上就是元数据,而这些都可以用注解取代,因为注解也是元数据。比如@Component注解标注在一个类上就可以说明该类是一个bean,@Autowire标注在成员变量上就直接给该成员变量赋值。看起来注解标注在代码上耦合性更强了,但实际上本着约定优于配置的原则,代码更加清晰,也容易维护。

元注解:
元注解是什么意思呢?

元注解是可以在你写的自定义注解(@interface)里面再写注解,或者说元注解是一种基本注解,但是它能够应用到其它的注解上面。

注解分类:
注解大体上可以分成三类,一类是系统带的如@Override,这类注解对其标注的目标的处理在JVM层面进行;另一类是我们自定义的注解,自己定义的注解自己要进行解析。还有一类是描述注解的注解(或者说 元注解),共有4个,分别是:
1、@Documented 注解信息是否添加到javadoc中
2、@Retention 注解的生命周期

package java.lang.annotation;

/**
 * Annotation retention policy.  The constants of this enumerated type
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation type to specify
 * how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    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,

    /**
     * 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
}

RetentionPolicy.SOURCE(编译结束后失效如@Override),
RetentionPolicy.CLASS(JVM加载类的时候失效,默认。不能通过反射获取),RetentionPolicy.RUNTIME(始终不失效,一般用于自定义注解,可通过反射获取)

3、@Target 注解用在什么地方,分别有几个地方:

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
}

ElementType.TYPE:类上
ElementType.FIELD:成员变量
ElementType.METHOD: 方法
ElementType.PARAMETER: 参数
ElementType.CONSTRUCTOR: 构造方法
ElementType.LOCAL_VARIABLE: 本地变量
ElementType.ANNOTATION_TYPE: 另一个注释
ElementType.PACKAGE: 包上
ElementType.TYPE_PARAMETER: 表示该注解能使用在自定义类型参数(参数的自定义类型可以是javaBean或者枚举等)的声明语句中。
ElementType.TYPE_USE: 表示该注解能使用在使用类型的任意语句中。

4、@Inherited 注解作用被子类继承,Inherited 是继承的意思,但是它并不是说注解本身可以继承,而是说如果一个超类(是注解接口)被 @Inherited 注解过的话,那么如果它的子类没有被任何注解应用的话,那么这个子类就继承了超类的注解。假如类B继承了A,则B也会带上该注解。

可以这样理解:
张三非常有钱,所以人们给他贴了一张标签叫做富豪。

张三的儿子长大后,只要没有和老子断绝父子关系,虽然别人没有给他贴标签,但是他自然也是富豪。

张三的孙子长大了,自然也是富豪。

这就是人们口中戏称的富一代,富二代,富三代。虽然叫法不同,好像好多个标签,但其实事情的本质也就是他们有一张共同的标签,也就是张三身上的那张富豪的标签。

注解的编写:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test1 {
    String value() default "我是一个注解";
}
@Test1("验证注解的本质")
public class main {
    public static void main(String[] args) {
        Annotation[] declaredAnnotations = main.class.getDeclaredAnnotations();
        Test1 declaredAnnotation = main.class.getDeclaredAnnotation(Test1.class);
        System.out.println(declaredAnnotation.value());
    }
}

结果:
1.0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值