注解简介:

注解Annotation是jdk1.5的新增功能,在现在的日常开发中,几乎离不开注解,写篇短文,来做个拾遗。

注解作用:

Annotation(注解)的作用是修饰包、类、构造方法、方法、成员变量等。

注解语法及定义形式:

  @interface关键字定义
  注解包含成员,成员以无参数的方法的形式被声明。其方法名和返回值定义了该成员的名字和类型。
  成员赋值是通过@Annotation(name=value)的形式
  注解需要标明注解的生命周期,注解的修饰目标等信息,这些信息是通过元注解实现。

  举例:

@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();
}

元注解@Retention,成员value的值为RetentionPolicy.RUNTIME、
       元注解@Target,成员value是个数组,用{}形式赋值,值为ElementType.ANNOTATION_TYPE
       成员名称为value,类型为ElementType[]

注解分类:

内置标准注解:

    @Override:用于修饰此方法覆盖了父类的方法
     @Deprecated:用于修饰已经过时的方法
     @SuppressWarnnings:用于通知java编译器禁止特定的编译警告

元注解:

    @Target:说明Annotation所修饰的对象范围
    @Retention:说明Annotation的生命周期
    @Documented :Documented是一个标记注解,主要是用于javadoc
    @Inherited :Inherited也是一个标记注解,假如一个标记了Inherited的注解修饰一个类,那么继承于此类的子类,同样继承该注解
          这样虽然完成了继承,但是注解本身是不允许继承的。

普通注解:

    自定义的完成特定任务的注解

注解生命周期:

    注解的生命周期是由元注解Retention修饰的
    看下Retention的源码

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
} 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
}

SOURCE: 注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃
CLASS: 注解被保留到class文件,jvm加载class文件时候被遗弃。这是默认的生命周期
RUNTIME: 注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在,保存到class对象中,可以通过反射来获取

注解的修饰目标:

  注解的修饰目标是由元注解Target修饰的

@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();
} 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
}

TYPE:指的是在类,接口(包括注解)或者enum上使用的注解
FIELD:指的在field属性,也包括enum常量使用的注解
METHOD:指的是在方法声明上使用的注解
PARAMETER:指的是在参数上使用的注解
CONSTRUCTOR: 指的是在构造器使用的注解
LOCAL_VARIABLE:指的是在局部变量上使用的注解
ANNOTATION_TYPE:指的是在注解上使用的元注解
PACKAGE:指的是在包上使用的注解

自定义注解:

  自定义注解是利用注解方面很重要的一个方面,这次把项目里的一个自定义的注解分析下
代码如下:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogMethod {
} @Slf4j
public class LogMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
String className = String.valueOf(methodInvocation.getMethod().getDeclaringClass());
String methodName = methodInvocation.getMethod().getName();
Object[] arguments = methodInvocation.getArguments();
log.info("Before:" + className + "." + methodName + "()");
log.info("Arguments:" + JsonTools.toJsonString(arguments));
long start = System.currentTimeMillis();
Object resultObj = methodInvocation.proceed();
long end = System.currentTimeMillis();
log.info("After:result is " + JsonTools.toJsonString(resultObj) + ",the execute time is " + (end - start) + " ms"); return resultObj;
}
}

具体配置:

<bean id="logMethodInterceptor" class="com.*LogMethodInterceptor"/>
<aop:config proxy-target-class="true">
<aop:pointcut id="logMethodPointcut"
expression="execution(* com*service.impl.*.*(..)) and @annotation(com.*annotation.LogMethod)"/>
<aop:advisor advice-ref="logMethodInterceptor" pointcut-ref="logMethodPointcut"/>
</aop:config>

LogMethod 定义了一个 生命周期为RUNTIME 作用在METHOD上的注解
LogMethodInterceptor 注解的处理器
利用Spring的AOP完整在方法的出入口分别打印参数的相关日志

Java注解拾遗的更多相关文章

  1. Java基础拾遗(二)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76358523冷血之心的博客) 马上就要秋招了,新的一轮笔试面试马上 ...

  2. Java注解

    Java注解其实是代码里的特殊标记,使用其他工具可以对其进行处理.注解是一种元数据,起到了描述.配置的作用,生成文档,所有的注解都隐式地扩展自java.lang.annotation.Annotati ...

  3. 19.Java 注解

    19.Java注解 1.Java内置注解----注解代码 @Deprecated                                    //不推荐使用的过时方法 @Deprecated ...

  4. Java注解入门

    注解的分类   按运行机制分:   源码注解:只在源码中存在,编译后不存在 编译时注解:源码和编译后的class文件都存在(如@Override,@Deprecated,@SuppressWarnin ...

  5. java注解(Annotation)解析

    注解(Annotation)在java中应用非常广泛.它既能帮助我们在编码中减少错误,(比如最常见的Override注解),还可以帮助我们减少各种xml文件的配置,比如定义AOP切面用@AspectJ ...

  6. JAVA 注解的几大作用及使用方法详解

    JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...

  7. attilax.java 注解的本质and 使用最佳实践(3)O7

    attilax.java 注解的本质and 使用最佳实践(3)O7 1. 定义pojo 1 2. 建立注解By eclipse tps 1 3. 注解参数的可支持数据类型: 2 4. 注解处理器 2 ...

  8. paip.java 注解的详细使用代码

    paip.java 注解的详细使用代码 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  9. JAVA 注解的几大作用及使用方法详解【转】

    java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单.注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后 某个时刻方便地使用这些数据(通过 解 ...

随机推荐

  1. 前端传给后端的数据类型为ImmutableMultiDict 咋办

    https://segmentfault.com/q/1010000002802028 偷得人家的答案     以下是解决办法:::: -------------------------------- ...

  2. 【vue】——vue.js 获取当前 自定义属性值

    假设有一个标签h5, 我们给它添加了一个自定义属性值,(item.id是从动态添加的) 点击h5 标签,如何才能获取当前对应的自定义属性值呢? 想当然的我最开始这样写: <h5 class=&q ...

  3. clang命令理解程序

    Xcode 创建一个mac OS   command Line Tool程序 步骤打开终端  cd + 工程路径(绝对路径)(注:拖拽main.m文件到终端) input —preprocessor— ...

  4. 性能测试 vs 负载测试 vs 压力测试

    在做一些软件测试工作时,常常会被提及性能测试.负载测试.压力测试,这也是在软件测试方面最容易混淆的三个概念.之前和一个测试大牛聊天,他和我说常常面试一些测试人员会问一些这样的问题,大多人认为负载测试等 ...

  5. 并发编程>>四种实现方式(三)

    概述 1.继承Thread 2.实现Runable接口 3.实现Callable接口通过FutureTask包装器来创建Thread线程 4.通过Executor框架实现多线程的结构化,即线程池实现. ...

  6. Django环境搭建之hello world

    当我们想用Python来开发一个web应用时,首先要选择一个优秀的web框架,Django是个非常成熟的web开发框架,网上具有丰富的文档和学习资料,所以选择Django框架来入门web开发是个不错的 ...

  7. BZOJ AC 300祭!

  8. Linux网络编程服务器模型选择之循环服务器

    在网络程序里面,通常都是一个服务器处理多个客户机,为了出个多个客户机的请求,服务器端的程序有不同的处理方式.本节开始介绍Linux下套接字编程的服务器模型选择,主要包括循环服务器模型.并发服务器模型. ...

  9. 关于javascript的各种高宽

  10. Idea与Eclipse操作代码的快捷方式

    1.Idea格式化代码的快捷键:ctrl+alt+L 2.在IDEA中创建了properties文件,发现默认中文不会自动进行unicode转码.如下 在project settings - File ...