其他注解的使用

ps:

java中元注解有四个: @Retention @Target @Document @Inherited;

   @Retention:注解的保留位置         

      @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含

      @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,

      @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到  

  @Target:注解的作用目标        

        @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) ///包   

     @Document:说明该注解将被包含在javadoc中

   @Inherited:说明子类可以继承父类中的该注解

 

 

一、@PostConstruct

@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}

 

使用示例:

 

@PostConstruct
	public void afterPropertiesSet() throws Exception {
		ActiveMQHelper.receiveFromDurableTopic(MessageEnum.paymentsuccess.value(), this,ModuleNameEnum.mz_topic.value());
	}

此注解用于方法上,用于执行依赖注入完成后的一些初始化操作。

这个方法会在类被投入服务之前被调用。

这个注解在所有支持依赖注入的类上都被支持。即使所在类不需要任何初始化的操作,被@PostConstruct注解的方法也一定会被调用。一个类中仅限于一个方法使用此注解有效。

被@PostConstruct的方法需要满足以下规则:

除去拦截器(可调用一个由拦截器规范定义的上下文对象)的情况外,该方法不能包含任何参数。

1、拦截器的方法:必须包含以下方法签名中的一个:

    1)返回为空的方法

    2)返回对象(不为空)方法声明抛出异常

 

    注:一个被@PostConstruct注解的拦截器方法不能抛出应用程序异常,

    但是拦截器方法除了生命周期事件之外,还会插入业务或超时方法,则它可以抛出检查异常;

    拦截器方法返回一个值,它将被容器忽略。

 

  ps:所有的检查性异常都继承自java.lang.Exception;所有的非检查性异常都继承自java.lang.RuntimeException。

         检查性异常和非检查性异常最主要的区别在于其处理异常的方式:检查性异常必须使用try catch或者throws等关键字进行处理,否则编译器会报错;非           检 查性异常一般是程序代码写的不够严谨而导致的问题,可以通过修改代码来规避。

常见的非检查性异常:空指针异常(NullPointerException)、除零异常(ArithmeticException)、数组越界异常(ArrayIndexOutOfBoundsException)等;

常见的检查性异常:输入输出异常(IOException)、文件不存在异常(FileNotFoundException)、SQL语句异常(SQLException)等。

2、非拦截器的方法:必须包含以下方法签名:

        1)返回为空的方法

       2)可被public, protected, package private 或 private修饰

       3)除了应用程序客户端之外,方法不能是静态的。

       4)方法可以是final的

       5)如果方法抛出一个非检查异常,则类不能被放入service服务中,除非EJB可以处理异常,甚至可以从它们恢复。

二、@PreDestroy

    

@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PreDestroy {
}

预删除注释作为回调通知在方法上使用,以指示实例正在被容器移除的过程中。带有预删除注释的方法通常用于释放它一直持有的资源。除了JavaEE 5中的应用程序客户端容器之外,支持PostConstruct的所有容器托管对象都必须支持此注释。应用预销毁注释的方法必须满足以下所有条件:
除去拦截器(可调用一个由拦截器规范定义的上下文对象)的情况外,该方法不能包含任何参数。

方法定义的其他要求 同@PostConstruct;

 

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.注释将由编译器记录在类文件中,但VM不需要在运行时保留。为默认类型
     */
    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.
     * 注释将由编译器记录在类文件中,并由VM在运行时保留,因此可以反射读取。
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}
  • @Retention指示要注释具有注释类型的注释的保留时间。 如果注释类型声明中没有保留注释,则保留策略默认为RetentionPolicy.CLASS

    保留元注释仅在元注释类型直接用于注释时才起作用。 如果元注释类型用作另一注释类型的成员类型,则它不起作用。

 

 

@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();
}
  • 指示注释类型适用的上下文。 注释类型可能适用的声明上下文和类型上下文在JLS 9.6.4.1中指定,并以源代码通过枚举常数java.lang.annotation.ElementType表示

  • 如果注释类型T上不存在@TargetT ,则可以将类型为T写为除了类型参数声明之外的任何声明的修饰符。

    如果存在@Target元注释,则编译器将强制使用ElementType枚举常量指定的使用限制,符合JLS 9.7.4。

 

 

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
}

 

 

 

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

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值