java注解学习---自定义注解定义java注解(二)

                           java注解学习---自定义注解定义java注解(二)

 

一、 自定义 @Target相关注解

1、@Target=ElementType.TYPE 支持类,接口,枚举注解

    

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.ANNOTATION_TYPE 支持 注解类型 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:11:21
 *
 */
@Target({ElementType.ANNOTATION_TYPE})
@Documented
public @interface HaHaTargetAnnotationType {

}

2、@Target=ElementType.FIELD 支持 字段,枚举注解
    

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.FIELD 支持 字段,枚举注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午12:59:34
 *
 */
@Target(ElementType.FIELD)
@Documented
public @interface HaHaTargetField {

}

   3、@Target=ElementType.METHOD 支持 方法 注解
       

 package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.METHOD 支持 方法 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:04:07
 *
 */
@Target(value={ElementType.METHOD})
@Documented
public @interface HaHaTargetMethod {


}

4、@Target=ElementType.PARAMETER 支持 方法参数 注解

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.PARAMETER 支持 方法参数 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:06:02
 *
 */
@Target(ElementType.PARAMETER)
@Documented
public @interface HaHaTargetParameter {

}

5、@Target=ElementType.CONSTRUCTOR 支持 构造方法 注解

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.CONSTRUCTOR 支持 构造方法 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:07:19
 *
 */
@Target(ElementType.CONSTRUCTOR)
@Documented
public @interface HaHaTargetConstructor {

}

6、@Target=ElementType.LOCAL_VARIABLE 支持 局部变量 注解

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.LOCAL_VARIABLE 支持 局部变量 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:09:49
 *
 */
@Target(ElementType.LOCAL_VARIABLE)
@Documented
public @interface HaHaTargetLocalVariable {

}

7、@Target=ElementType.ANNOTATION_TYPE 支持 注解类型 注解

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.ANNOTATION_TYPE 支持 注解类型 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:11:21
 *
 */
@Target({ElementType.ANNOTATION_TYPE})
@Documented
public @interface HaHaTargetAnnotationType {

}

8、@Target=ElementType.PACKAGE 支持 包 注解

 

package com.haha.study.annotation.target;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * @description: 自定义注解 --- @Target=ElementType.PACKAGE 支持 包 注解
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:14:03
 *
 */
@Target({ElementType.PACKAGE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HaHaTargetPackage {
	
	String version() default "1.0";
}

 

二、定义 @Retention相关注解

1、@Retention=RetentionPolicy.SOURCE  注解 不会被编译到 .class 文件中

package com.haha.study.annotation.retention;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
 * @description: 自定义注解 --- @Retention=RetentionPolicy.SOURCE  注解 不会被编译到 .class 文件中
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:16:24
 *
 */
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface HaHaRetentionSource {

}

2、@Retention=RetentionPolicy.CLASS  注解会被编译到 .class文件中,但不会被虚拟机加载。 (缺省 默认这个)
    

package com.haha.study.annotation.retention;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
 * @description: 自定义注解 --- @Retention=RetentionPolicy.CLASS  注解会被编译到 .class文件中,但不会被虚拟机加载
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:36:51
 *
 */
@Retention(RetentionPolicy.CLASS)
@Documented
public @interface HaHaRetentionClass {

}

3、@Retention=RetentionPolicy.RUNTIME  注解会被编译到 .class文件中,且也会被虚拟机加载到内存中 ---> 可通过反射获取注解信息
     

package com.haha.study.annotation.retention;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
 * @description:自定义注解 --- @Retention=RetentionPolicy.RUNTIME  注解会被编译到 .class文件中,且
 *  也会被虚拟机加载到内存中 ---> 可通过反射获取注解信息
 * @version:v1.0
 * @author:w
 * @date:2018年6月17日下午1:39:22
 *
 */
@Retention(value=RetentionPolicy.RUNTIME)
@Documented
public @interface HaHaRetentionRuntime {

}

三、使用自定义注解

package com.haha.study.annotation;
import com.haha.study.annotation.documented.HaHaDocumented;
import com.haha.study.annotation.inherited.HaHaInherted;
import com.haha.study.annotation.retention.HaHaRetentionRuntime;
import com.haha.study.annotation.target.HaHaTargetConstructor;
import com.haha.study.annotation.target.HaHaTargetField;
import com.haha.study.annotation.target.HaHaTargetLocalVariable;
import com.haha.study.annotation.target.HaHaTargetMethod;
import com.haha.study.annotation.target.HaHaTargetParameter;
import com.haha.study.annotation.target.HaHaTargetType;

/**
 * description 基于四个元注解,创建的自定义注解,测试。 @HaHaTargetPackage 单独讲解. (description)
 * @version v1.0
 * @author w
 * @date 2018年6月17日下午7:01:39
 *
 */
@HaHaTargetType
@HaHaDocumented
@HaHaInherted
@HaHaRetentionRuntime
public class AnnotationDemo {
	@HaHaTargetField
	private String userName;
	/**
	 * @description 这是一个构造方法!
	 * @see this constructor
	 * @param userName
	 */
	@HaHaTargetConstructor
	public AnnotationDemo(@HaHaTargetParameter String userName){
		this.userName=userName;
	}
	/**
	 * @description 普通方法测试。
	 * @param no params
	 * @return 没有返回值
	 * @version v1.0
	 * @author w
	 * @date 2018年6月17日 20:16:43
	 *
	 */
	@HaHaTargetMethod
	public void sayHello(){
		@HaHaTargetLocalVariable
		String  info="userName : "+userName;
		System.out.println(info);
	}
	public static void main(String[] args) {
		AnnotationDemo demo=new AnnotationDemo("xiaoMing");
		demo.sayHello();
	}
}

 

 

 

四、总结

1、使用四种元注解,自定义注解注意几点:

  • @Target(ElementType.?) 根据实际需求定义,不建议定义为 @HaHaTargetAll
  • 注解定义为: @Retention(RetentionPolicy.RUNTIME) 便于反射中获取,除非这个注解

作用类似于 @Override 用于编译前限定。

  • @Documented 添加上,便于生成 javadoc 。 参考 jdk内置三个注解都加上了。
  • @Inherited 添加上,当前可能是不需要的,指不定两个被注解类就存在继承关系。
  • 上面演示的注解 @HaHa** 为了便于理解,都没在注解中定义元素。 注解中定义元素格式

为: 注解元素类型 元素名() [ default 元素值 ] , 如: int order() default 1; 其中元素名为 value 时,在使用中可以省略不写。

 

 

 

 

 

---- 没看懂,回去看看 :

    java注解学习---元注解,注解类型,注解支持元素类型,注解基础(一)

---- 继续学习 next :

java注解使用、java获取注解的属性、获取注解的字段值   (新增)

java注解学习---@Target({ElementType.PACKAGE})理解和使用(三)

 

   java注解学习---@Inherited注解的理解学习(四)   

 

 

 

参考资料: http://zy19982004.iteye.com/blog/1979208

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值