`

Annotation一点总结

阅读更多

原写于2011-06-25

 

题记:建议关于spring问题,请记得查看spring reference

 

一、annotation前生后世

Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.

译:annotation不会直接影响程序的语义,xxxAnnotation可以从源文件、class文件、通过反射在运行时读取。

 

参考:http://www.developer.com/print.php/3556176

1.定义

Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, StringClassenums, annotations, and arrays of the preceding types. Methods can have default values.

Annotation声明与普通的interface非常相似,在关键字interface前加@。每一个方法的声明定义一个annotation的元素。方法不能有任何的参数或throws异常。返回类型被限制为:原始类型、StringClassenumannotation、前面描述的类型组成的数组。method定义允许有默认值。

 

2.Java annotation

There are two types of annotations available with JDK5:

Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.

三个基本的annotation,如:OverrideDeprecatedSuppresswarnings,不能使用它去定义新的annotation

Meta annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.

annotation,定义annotation的类型。如:TargetRetentionDocumentedInherited

- Target:声明annotation注解的目标类型。如@Target(ElementType.TYPE)@Target(ElementType.METHOD)

- Retention:声明annotation被保留的长度。如:RetentionPolicy.SOURCERetentionPolicy.CLASSRetentionPolicy.RUNTIME

- Documented:声明被注解的target生成doc是否需要显示annotation信息。

- Inheritedxxx

 

3.annotation作用

不影响程序原本语义的情况下,增加信息+工具=声明式编程。如:spring aop

 

 

二、利用annotation实现aop

1. 思路

A.自定义注解定义规则(何时执行)

B.标记行为(执行什么)

C.通过反射生成代理,在对象执行任何方法时,进行拦截判断是否符合规则;若符合,执行对应的行为。

2. 例子

场景:一个表演家,表演节目后,观众拍手鼓掌

原始:表演家拥有所有观众的引用,在自己表演完后,通知观众鼓掌

问题:表演家应该关注表演自身的事情,有哪些观众、观众是否鼓掌,不是其所关注的

改进:AOP方式,表演家仅关注表演,观众鼓掌由其它人负责

总结:

面向切面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP)。 面向对象将应用程序分解成各个层次的对象,而AOP将程序分解成各个切面或者说关注点。这使得可以模块化诸如事务管理等这些横切多个对象的关注点。

 

 

三、spring aop如何使用annotation

1. 基本方式,通过ProxyFactoryBean

a.通知

b.切入点

b.通知+切入点 à切面

c.实际对象+接口+切面 à接口代理

2. 自动代理方式:

基本方式的问题是,xml配置文件繁琐。

a.基于spring上下文的通知者Bean的自动代理(利用PostBeanProcessor

BeanNameAutoProxyCreator,有属性:beanNames(被代理对象)、interceptorNames(通知)。自动代理beanNames指定的bean,此时interceptorNames指定通知,但interceptor需要实现特定的接口,如:MethodBeforeAdvice

b.基于aspectJ注解驱动

使用aspectJ风格的注解。原理:生成代理,仅方法级别。使用AspectJ 提供的一个库来做切点(pointcut)解析和匹配。

c.<aop:config>,优点:任何类都能转化为切面,不需要特殊接口或注解,原始pojo对象

aop参数传递,见:spring reference  6.3.3.6 通知参数

around切点,必须传递ProceedingJoinPoint参数,通过ProceedingJoinPoint取得方法的所有信息。

其它切点,利用JoinPoint取得方法的所有参数。(通过参数名绑定传递参数,未理解)

 

四、annotation相关技术

1.cglib

它的原理就是用Enhancer生成一个原有类的子类,并且设置好callbackproxy 则原有类的每个方法调用都会转为调用实现了MethodInterceptor接口的proxyintercept() 函数。

2.asm

ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or dynamically generate classes, directly in binary form. Provided common transformations and analysis algorithms allow to easily assemble custom complex transformations and code analysis tools.

ASM offer similar functionality as other bytecode frameworks, but it is focused on simplicity of use and performance. 

简单理解asm是比cglib更高级的code generate lib

 

五、others问题

1.Generics 

Generics - This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting. See theGenerics Tutorial. (JSR 14)

泛型提供了编译时类型检查安全。这点很重要吗

 

2.为什么spring aop aspectJ anonotationpointcut声明,必须注解在方法上 ?通过方法唯一标识一个pointcut

猜测:@PointcutTarget属性指定仅为method。方法签名成为pointcutid

 

 

分享到:
评论

相关推荐

    arcgis工具

    arcgis工具总结 1. 要素的剪切与延伸 实用工具 TASK 任务栏 Extend/Trim feature 剪切所得内容与你画线的方向有关。 2. 自动捕捉跟踪工具 点击Editor工具栏中Snapping来打开Snapping Environment对话框 捕捉设置...

    hibernate 框架详解

    使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...

    hibernate3.04中文文档.chm

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    Hibernate教程

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    Hibernate中文详细学习文档

    1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得...

    Hibernate参考文档

    1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得SessionFactory 3.3...

    hibernate 体系结构与配置 参考文档(html)

    使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate+中文文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    Hibernate 中文 html 帮助文档

    1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得SessionFactory 3.3...

    HibernateAPI中文版.chm

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    hibernate3.2中文文档(chm格式)

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    springmybatis

    后来项目结束了,我也没写总结文档。已经过去好久了。但最近突然又对这个ORM 工具感兴趣。因为接下来自己的项目中很有可能采用这个ORM工具。所以在此重新温习了一下 mybatis, 因此就有了这个系列的 mybatis 教程. ...

    Activiti6.0教程例子下载

    其中大部分文字来自Copy网上的各种资料与文档,通过总结而来的。具体的更详细的内容需自己google,参考一些官方的文档与手册。 本文档之后内容如下: 1) 下载与使用 2) 核心组件与说明 3) 入门示例 4) Eclipse中的...

Global site tag (gtag.js) - Google Analytics