A、术语
A.1、AspectJ
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
A.2、@ AspectJ
@ AspectJ使用Java 5的注解,可以将切面声明为普通的Java类。
B、Spring和@ AspectJ
B.1、Spring中的@ AspectJ
Spring 2.0使用了和AspectJ 5一样的注解,但是AOP在运行是仍然是纯的Spring AOP,并不依赖AspectJ的编译器和织入器。
B.2、启用@ AspectJ
1.若使用DTD,可在applicationContext.xml中添加定义来启用@ AspectJ支持
A、术语
A.1、AspectJ
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
A.2、@ AspectJ
@ AspectJ使用Java 5的注解,可以将切面声明为普通的Java类。
B、Spring和@ AspectJ
B.1、Spring中的@ AspectJ
Spring 2.0使用了和AspectJ 5一样的注解,但是AOP在运行是仍然是纯的Spring AOP,并不依赖AspectJ的编译器和织入器。
B.2、启用@ AspectJ
1.若使用DTD,可在applicationContext.xml中添加定义来启用@ AspectJ支持
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://schema/beans" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema/beans <bean class="org.springframework.aop.aspectj.annotation.AnnotationAware</bean> http://schema/beans/spring-beans-2.5.xsd"> AspectJAutoProxyCreator"> 6 7 </beans>
2.若使用XML Schema-based,添加以下定义来启用@ AspectJ支持。
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://schema/beans" 4 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 7 xmlns:aop="http://schema/aop" 8 9 xsi:schemaLocation=" 10 11 http://schema/beans 12 13 http://schema/beans/spring-beans-2.5.xsd http://schema/aop 14 15 http://schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy /> 16 17 <bean id="studentAspect" class="spring.chapter06.demo1.StudentAspect"> </bean> <bean id="student" class="spring.chapter06.demo1.Student"></bean> </beans>
C、@ AspectJ配置
C.1、Aspect
声明一个切面(Aspect)
在application context中定义的任何带有@Aspect切面(有@Aspect注释)的bean都被Spring识别并用于配置Spring AOP。
带有@Aspect注释的类
1 package spring.chapter06.demo1; 2 3 import org.aspectj.lang.annotation.Aspect; 4 5 @Aspect 6 7 public class NotVeryUsefulAspect { 8 9 } 10 11 application context中的bean 12 13 <bean id="myAspect" 14 15 class="spring.chapter06.demo1.NotVeryUsefulAspect"> </bean>
C.2、PointCut
一个切入点PointCut声明有两部分:一个包含名字和参数的签名(也就是java方法),一个是PointCut表达式
@Pointcut ("execution (* transfer(..))")
public void anyOldTransfer(){}
C.3、Advice
Advice是和PointCut表达式关联起来的,并在PointCut匹配的方法执行之前或者之后或者前后运行。
1 import org.aspectj.lang.annotation.Aspect; 2 3 import org.aspectj.lang.annotation.Before; 4 5 @Aspect 6 7 public class BeforeExample { 8 9 } 10 11 @Before public void doAccessCheck(){} ("spring.chapter06.demo1.NotVeryUsefulAspect.anyOldTransfer()")
C.4、Introduction
在AspectJ中成为inter-type声明,允许一个切面Aspect可以定义一个被通知对象Target Object实现指定接口。
D、举例说明
范例1:
我们还是回到《[spring]2 初识spring和AOP》中的例子中,看用@Aspect如何实现。
C.1、先定义目标对象Target Object的类
这里我们为了更精简的,不再创建接口,我们现在为了说明AOP,而接口的做法更属于IoC的范畴。
public class Student {
} public void addStudent(String name) { System.out.println("欢迎"+name+"加入Spring家庭! "); }
在AOP领域内,Target Obejct还是很容易被区分出来的。
C.2、定义Aspect、PointCute和Advice
1 import org.aspectj.lang.ProceedingJoinPoint; 2 3 import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; 4 5 @Aspect 6 7 public class StudentAspect { 8 9 @Around("check() && args(name,..)") public void aroundCheck(ProceedingJoinPoint joinpoint, String name) throws Throwable { joinpoint.proceed(); if (name.equals("dragon")) { System.out.println("批准其加入."); } else { System.out.println("此学生是" + name + ",不批准其加入."); } @AfterReturning("check()") public void afterCheck() { } System.out.println("After Advice"); @Before("check()") public void beforeCheck() { } System.out.println("Before Advice"); @Pointcut("execution(* public void check() { } spring.chapter06.demo1.Student.addStudent(..))") 10 11 } }
这里很直观的声明了一个切面Aspect和切面的切点PointCut和切点所采用的处理逻辑Advice。
C.3、配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://schema/beans" 4 5 class="org.springframework.aop.aspectj.annotation.AnnotationAware<bean id="studentAspect" class="spring.chapter06.demo1.StudentAspect"> </bean> <bean id="student" class="spring.chapter06.demo1.Student"></bean> AspectJAutoProxyCreator" /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema/beans <bean http://schema/beans/spring-beans-2.5.xsd"> </beans>
这里定义了两个bean一个是目标对象Target Object,一个是切面Aspect。而Aspect具体的设置交由@AspectJ来设定。
浙公网安备 33010602011771号