石建 | Fat Mind

Annotation一点总结

Annotation

 

题记:建议关于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:

1) 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

2) 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

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

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

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

D.      Inheritedxxx

 

3.       annotation作用

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

b. 编译检查

 

 

二、利用annotation实现aop

1. 思路

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

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

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

2. 例子

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

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

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

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

总结:

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

 

三、spring aop如何使用annotation

1. 基本方式,通过ProxyFactoryBean

a.通知

b.切入点

b.通知+切入点 à 切面

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

2. 自动代理方式:

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

1)基于spring上下文的通知者Bean的自动代理(利用PostBeanProcessor ?)

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

2) 基于aspectJ注解驱动

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

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

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

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

b. 其它切点,利用JoinPoint取得方法的所有参数。

c. 通过参数名绑定传递参数,未理解

 

 

四、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

  

 

posted on 2011-06-25 20:09 石建 | Fat Mind 阅读(320) 评论(0)  编辑  收藏 所属分类: 一点理解


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

What 、How、Why,从细节中寻找不断的成长点