Spring AOP+AspectJ注解实例
文章目录
本文展示如何将AspectJ注解集成到Spring AOP框架。在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法。
常见AspectJ的注解:
- @Before – 方法执行前运行
- @After – 运行在方法返回结果后
- @AfterReturning – 运行在方法返回一个结果后,在拦截器返回结果。
- @AfterThrowing – 运行方法在抛出异常后,
- @Around – 围绕方法执行运行,结合以上这三个通知。
注意
Spring AOP 中没有 AspectJ 支持,请阅读前文AOP面向切面编程。
1. 启用AspectJ
在 Spring 配置文件,把“<aop:aspectj-autoproxy />”,并定义Aspect(拦截)和普通的bean。
File : applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.myprj.customer.bo.impl.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.myprj.aspect.LoggingAspect" />
</beans>
2. AspectJ @Before
在下面例子中,logBefore()方法将在 customerBo接口的 addCustomer()方法的执行之前被执行。
AspectJ的“切入点”是用来声明哪种方法将被拦截],支持切入点表达式的完整列表。
File : LoggingAspect.java
package com.myprj.aspect;
import org.aspectj.lang.Joinmyprj;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(* com.myprj.customer.bo.CustomerBo.addCustomer(..))")
public void logBefore(Joinmyprj joinmyprj) {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinmyprj.getSignature().getName());
System.out.println("******");
}
}
运行
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomer();
输出结果
logBefore() is running!
hijacked : addCustomer
******
addCustomer() is running
3. AspectJ @After
在下面例子中,logAfter()方法将在 customerBo 接口的 addCustomer()方法的执行之后执行。
File : LoggingAspect.java
package com.myprj.aspect;
import org.aspectj.lang.Joinmyprj;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;
@Aspect
public class LoggingAspect {
@After("execution(* com.myprj.customer.bo.CustomerBo.addCustomer(..))")
public void logAfter(Joinmyprj joinmyprj) {
System.out.println("logAfter() is running!");
System.out.println("hijacked : " + joinmyprj.getSignature().getName());
System.out.println("******");
}
}
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomer();
输出结果
addCustomer() is running
logAfter() is running!
hijacked : addCustomer
******
4. AspectJ @AfterReturning
在下面例子中,logAfterReturning()方法将在 customerBo 接口的addCustomerReturnValue()方法执行之后执行。此外,还可以截取返回的值使用“returning”属性。
要截取返回的值,对“returning”属性(结果)的值必须用相同的方法参数(结果)。
File : LoggingAspect.java
package com.myprj.aspect;
import org.aspectj.lang.Joinmyprj;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;
@Aspect
public class LoggingAspect {
@AfterReturning(
pointcut = "execution(* com.myprj.customer.bo.CustomerBo.addCustomerReturnValue(..))",
returning= "result")
public void logAfterReturning(Joinmyprj joinmyprj, Object result) {
System.out.println("logAfterReturning() is running!");
System.out.println("hijacked : " + joinmyprj.getSignature().getName());
System.out.println("Method returned value is : " + result);
System.out.println("******");
}
}
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomerReturnValue();
输出结果
addCustomerReturnValue() is running
logAfterReturning() is running!
hijacked : addCustomerReturnValue
Method returned value is : abc
******
5. AspectJ @AfterReturning
在下面的例子中,如果 customerBo 接口的addCustomerThrowException()方法抛出异常logAfterThrowing()方法将被执行。
File : LoggingAspect.java
package com.myprj.aspect;
import org.aspectj.lang.Joinmyprj;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;
@Aspect
public class LoggingAspect {
@AfterThrowing(
pointcut = "execution(* com.myprj.customer.bo.CustomerBo.addCustomerThrowException(..))",
throwing= "error")
public void logAfterThrowing(Joinmyprj joinmyprj, Throwable error) {
System.out.println("logAfterThrowing() is running!");
System.out.println("hijacked : " + joinmyprj.getSignature().getName());
System.out.println("Exception : " + error);
System.out.println("******");
}
}
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomerThrowException();
输出结果
addCustomerThrowException() is running
logAfterThrowing() is running!
hijacked : addCustomerThrowException
Exception : java.lang.Exception: Generic Error
******
Exception in thread "main" java.lang.Exception: Generic Error
//...
6. AspectJ @Around
在下面例子中,logAround()方法将在customerBo接口的addCustomerAround()方法执行之前执行, 必须定义“joinmyprj.proceed();” 控制何时拦截器返回控制到原来的addCustomerAround()方法。
File : LoggingAspect.java
package com.myprj.aspect;
import org.aspectj.lang.ProceedingJoinmyprj;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
@Aspect
public class LoggingAspect {
@Around("execution(* com.myprj.customer.bo.CustomerBo.addCustomerAround(..))")
public void logAround(ProceedingJoinmyprj joinmyprj) throws Throwable {
System.out.println("logAround() is running!");
System.out.println("hijacked method : " + joinmyprj.getSignature().getName());
System.out.println("hijacked arguments : " + Arrays.toString(joinmyprj.getArgs()));
System.out.println("Around before is running!");
joinmyprj.proceed(); //continue on the intercepted method
System.out.println("Around after is running!");
System.out.println("******");
}
}
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomerAround("myprj");
输出结果
logAround() is running!
hijacked method : addCustomerAround
hijacked arguments : [myprj]
Around before is running!
addCustomerAround() is running, args : myprj
Around after is running!
******
总结
如今总是建议采用最少 AspectJ 注解。这是关于Spring AspectJ 的一篇相当长的文章。进一步的解释和例子,请访问下面的参考链接。
Spring AOP+AspectJ在XML配置实例
在下文中,我们将向你展示如何转换上文中 Spring AOP+AspectJ注解转成基于XML的配置。
对于那些不喜欢注释,使用JDK1.4,则可以基于XML,而不使用 AspectJ。
完整的 XML 实例
查看完整的基于XML的 AspectJ 配置文件。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.myprj.customer.bo.impl.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.myprj.aspect.LoggingAspect" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect">
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.myprj.customer.bo.CustomerBo.addCustomer(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.myprj.customer.bo.CustomerBo.addCustomer(..))" />
<aop:after method="logAfter" pointcut-ref="pointCutAfter" />
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* com.myprj.customer.bo.CustomerBo.addCustomerReturnValue(..))" />
<aop:after-returning method="logAfterReturning"
returning="result" pointcut-ref="pointCutAfterReturning" />
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* com.myprj.customer.bo.CustomerBo.addCustomerThrowException(..))" />
<aop:after-throwing method="logAfterThrowing"
throwing="error" pointcut-ref="pointCutAfterThrowing" />
<!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(* com.myprj.customer.bo.CustomerBo.addCustomerAround(..))" />
<aop:around method="logAround" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
</beans>