(1) 定义注解:
package annotations;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DefineAnnotation {
public String hello() default "aaa";
String world();
}
(2)、使用注解
package annotations;
public class UseDefineAnnotation {
@Deprecated
@DefineAnnotation(hello="ttitfly",world="WORLD!!!")
public void say(){
System.out.println("say hello");
}
/* public static void main(String[] args) {
UseDefineAnnotation tAnnotation=new UseDefineAnnotation();
tAnnotation.say();
}*/
}
(3)、处理器 解析注解
package annotations;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestDefineAnnotation {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
UseDefineAnnotation useDefineAnnotation=new UseDefineAnnotation();
//1、要解析的方法带有注解的类
Class clazz=UseDefineAnnotation.class;
//Method method = clazz.getMethod("say", null);//因为是无参数的,所以直接写个null也可以
//2、获得带有注解的方法
Method method=clazz.getDeclaredMethod("say", new Class[]{});
method.invoke(useDefineAnnotation, new Class[]{}); //在实例myAnnotation上执行方法method()
//该方法是否使用了MyAnnotation这个注解类
boolean isExists=method.isAnnotationPresent(DefineAnnotation.class);
if(isExists){
//3、获得方法上的注解
/*Annotation在运行时刻,是通过java的动态代理实现的,
每次得到一个annotation,实际上并不是得到该annotation的实例,
而是得到了它的一个代理,这个代理java在命名上采用的是$Proxy1,
$Proxy2...的形式,数字是按照出现的顺序来定的 */
DefineAnnotation annotation=method.getAnnotation(DefineAnnotation.class);
//4、方法注解的element方法
System.out.println(annotation.hello()+" "+annotation.world());
}
System.out.println("-------------------");
/************************************************/
Method[] methods=clazz.getDeclaredMethods();
for(Method mt:methods){
Annotation[] annotations=mt.getAnnotations();
for(Annotation ant:annotations){
System.out.println(ant.annotationType().getName());
//不能通过getClass().getName()的方式获得了。这样获得的结果类似这样的:$Proxy3,主要原因是因为Annotation在运行时刻,是通过java的动态代理实现的,
//每次得到一个annotation,实际上并不是得到该annotation的实例,而是得到了它的一个代理,这个代理java在命名上采用的是$Proxy1,$Proxy2...的形式,数字是按照出现的顺序来定的
//而***getClass()方法返回的是该对象运行时刻所代表的真实对象***,在这里也就是代理对象了
System.out.println(ant.getClass().getName());
//输出结果为:java.lang.reflect.Proxy
System.out.println(ant.getClass().getSuperclass().getName());
}
}
}
}
(4)、注解的继承性 (Inherited)
注解定义时使用了@Inherit,注解用于Type(如class)上时,Type上的注解将被Type的派生类型继承到
package annotations;
import java.lang.annotation.*;
//没有指定@Target(),则注解可用于所有的ElementType
@Inherited
@Retention(RetentionPolicy.RUNTIME)
//在自己定义的Annotation里加入一个@Inherited ,就标识了这个定义的Annotation是可以被继承的
/*指示注释类型被自动继承。如果在注释类型声明中存在 Inherited 元注释,并且用户在某一类声明中查询该注释类型,
* 同时该类声明中没有此类型的注释,则将在该类的超类中自动查询该注释类型。此过程会重复进行,直到找到此类型的注释
* 或到达了该类层次结构的顶层 (Object) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。
注意,如果使用注释类型注释类以外的任何事物,此元注释类型都是无效的。还要注意,此元注释仅促成从超类继承注释;对已实现接口的注释无效。
*/
public @interface MyInherit {
String value();
}
package annotations;
@MyInherit("test class")
public class Parent {
@MyInherit("test method")
public void doSomething(){
System.out.println("hello");
}
}
package annotations;
public class Child extends Parent {
}
package annotations;
import java.lang.reflect.*;
public class TestInherit {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Class< Child > clazz = Child.class;
if(clazz.isAnnotationPresent(MyInherit.class)){
MyInherit myInherit = clazz.getAnnotation(MyInherit.class);
System.out.println(myInherit.value());
}
Method method = clazz.getMethod("doSomething", new Class[]{});
if(method.isAnnotationPresent(MyInherit.class)){
MyInherit myInherit = method.getAnnotation(MyInherit.class);
System.out.println(myInherit.value());
}
}
/*@Inherit指示注释类型被自动继承。如果在注释类型声明中存在 Inherited 元注释,并且用户在某一类声明中查询该注释类型,
* 同时该类声明中没有此类型的注释,则将在该类的超类中自动查询该注释类型。此过程会重复进行,直到找到此类型的注释或到达了
* 该类层次结构的顶层 (Object) 为止. Parent类上使用了注释@MyIherit,Parent的派生类Child将继承类上的注释@MyHerit (这就是@Inherit在@MyInherit上的作用).
注解@MyInherit用于方法Parent.doSomething()上,这个带指定注解@MyInherit的“***方法doSomething()***”
是否可以被Parent的派生类Child所继承到(@MyInherit不管有没有@Inherit,带有注解@MyInherit的方法
doSomething都可以被继承到),方法doSomething确实是被Child{}继承到了,自然方法上的注解是可以被得到的。*/
}
分享到:
相关推荐
一个简单的Spring MVC 框架搭建的示例 并且清楚的注解让你更快速的书写代码
NULL 博文链接:https://wangming2012.iteye.com/blog/1941123
详细介绍了springmvc的注解 如@Controller,@RequestMapping等,详细到你无法想象,采用例子代码的方式,一目了然。
主要介绍了详解Mybatis注解写法(附10余个常用例子),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本篇文章主要介绍了spring mvc(注解)上传文件的简单例子,具有一定的参考价值,有兴趣的可以了解一下。
最近有所了解到自定义注解的应用,因此学习了一下,下面这篇文章主要给大家介绍了关于Java中自定义注解介绍与使用场景的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧
springboot+mybatis plus整合案例,改改数据库直接启动
书中对每个开发包的功能、原理和安装步骤进行了介绍,对开发包的各种数据结构和输出函数进行了详细阐述,并通过举例对开发包的使用方法进行了详细说明,每个例子都给出了完整的源代码并有详细的注解和结果分析。...
举个更具体的例子,我们以前所定义的Bean,当Spring容器一启动的时候,就会一股脑的全部加载我们自定义配置的信息和完成对Bean的创建;而使用了Profile之后,它会将Bean的定义进行更细粒度的划分,将这些定义的Bean...
java多态的基础知识,你可能了解,但是如何使用?内附例子。究竟如何实现,帮你学习基础。
带有详细的例子和注解,Des和3Des的算法,建议到本人资源中再下载详解解释(免费的)
书中对每个开发包的功能、原理和安装步骤进行了介绍,对开发包的各种数据结构和输出函数进行了详细阐述,并通过举例对开发包的使用方法进行了详细说明,每个例子都给出了完整的源代码并有详细的注解和结果分析。...
Git在各个操作系统的安装过程就不缀文了,步骤都是固定的,按照步骤一步一步安装就可以了。在开始讲解之前,我们先对Git进行资源版本管理有个...(PS:下文中【】的内容为对命令的注解)此例子只会产生一次commitlog,
3 Spring基本功能详解 8 3.1 SpringIOC 8 3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化...
使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...
RxJava和Retrofit2用了一段时间了,写个小例子,分享出来,有什么不对的地方还请大神在评论区指正。 什么是Retrofit2 官网是这么介绍的: Retrofit adapts a Java interface to ...
学习定时任务调度工具详解quartz demo-04-springboot SpringBoot入门 demo-05-spring-annotation spring中常用注解 demo-06-java8 java8的一些新特性 demo-07-netty-tomcat 基于netty实现tomcat demo-08-netty-im ...
7.3.2 一个简单的例子 7.3.3 如何通过配置使用@AspectJ切面 7.4 @AspectJ语法基础 7.4.1 切点表达式函数 7.4.2 在函数入参中使用通配符 7.4.3 逻辑运算符 7.4.4 不同增强类型 7.4.5 引介增强用法 7.5 切点函数详解 ...
7.3.2 一个简单的例子 7.3.3 如何通过配置使用@AspectJ切面 7.4 @AspectJ语法基础 7.4.1 切点表达式函数 7.4.2 在函数入参中使用通配符 7.4.3 逻辑运算符 7.4.4 不同增强类型 7.4.5 引介增强用法 7.5 切点函数详解 ...
2.SpringBoot的入门例子 3.SpringBoot 常用配置 4.SpringBoot 自定义配置 5.SpringBoot的yml配置详解 大厂面试题:请讲下SpringBoot的日志框架原理? 配置日志的存储路径、设置日志的格式 02 在springboot中...