package aspect1; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * @author zhuc * */ @Aspect @Component public class Aspect1 { public Aspect1(){ System.out.println("Aspect1() 构造器"); } // @Pointcut(value = "execution(* add*(..)) || execution(* del*(..))") @Pointcut(value = "execution(* aspect1..*(..))") // 参数(..)可以写完整的package名+类名 // @Pointcut(value = "within(aspect1..*)") // @Pointcut(value = "within(aspect1.Service2Impl)") //匹配目标类Service1Impl(包名可以省略)的所有方法 // @Pointcut(value = "args(String)") private void pointCut() { } @Before(value = "pointCut()") public void doBefore() { System.out.println("doBefore......"); } @After(value = "pointCut()") public void doAfter() { System.out.println("doAfter......"); } /** * AfterReturning 增强处理处理只有在目标方法成功完成后才会被织入。<br> * After 增强处理不管目标方法如何结束(保存成功完成和遇到异常中止两种情况),它都会被织入。<br> * @param obj */ @AfterReturning(value = "pointCut()", returning = "obj") public void doAfterReturning(Object obj) { System.out.println("返回的值是:" + obj); System.out.println("doAfterReturning+Obj......"); } /** * Around 增强处理近似等于 Before 增强处理和 AfterReturning 增强处理的总和<br> * 它可改变执行目标方法的参数值,也可改变目标方法之后的返回值<br> * @param pjp * @return * @throws Throwable */ @Around(value = "pointCut()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("before doAround......"); Object obj = pjp.proceed(); System.out.println("doAround......"); System.out.println("after doAround......"); return obj; } /** * @param ex */ @AfterThrowing(value = "pointCut()", throwing = "ex") public void doAfterThrowing(Throwable ex) { System.out.println("抛出的异常是:" + ex); System.out.println("doAfterThrowing......"); } }
关于PointCut中execution表达式规范,请参考 http://zhuchengzzcc.iteye.com/blog/1504014
package aspect1; /** * @author zhuc * */ public interface Service1 { public void addDomain(); public void delDomain() throws Exception; public String modifyDomain(String name); }
package aspect1; public interface Service2 { public void buy(); }
package aspect1; /** * @author zhuc * */ public class Service1Impl implements Service1 { @Override public void addDomain() { System.out.println("Service1 添加"); } @Override public String modifyDomain(String name) { System.out.println("Service1 修改" + name); return name; } @Override public void delDomain() throws Exception { System.out.println("Service1 删除"); int i = 1 / 0; } }
package aspect1; public class Service2Impl implements Service2{ @Override public void buy() { System.out.println("Service2 买"); } }
aspect1.xml:
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <context:component-scan base-package="aspect1" /> <aop:aspectj-autoproxy /> <bean id="service1" class="aspect1.Service1Impl" /> <bean id="service2" class="aspect1.Service2Impl" /> </beans>
测试方法:
package aspect1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author zhuc * */ public class Test1 { /** * @param args * @throws Exception */ public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "aspect1/aspect1.xml"); Service1 s = (Service1) ac.getBean("service1"); s.addDomain(); System.out.println(); s.modifyDomain("哈哈"); System.out.println(); try { s.delDomain(); } catch (Exception e) { } System.out.println(); Service2 s2 = (Service2) ac.getBean("service2"); s2.buy(); } }
运行结果如下:
Aspect1() 构造器
doBefore......
before doAround......
Service1 添加
doAfter......
返回的值是:null
doAfterReturning+Obj......
doAround......
after doAround......
doBefore......
before doAround......
Service1 修改哈哈
doAfter......
返回的值是:哈哈
doAfterReturning+Obj......
doAround......
after doAround......
doBefore......
before doAround......
Service1 删除
doAfter......
抛出的异常是:java.lang.ArithmeticException: / by zero
doAfterThrowing......
doBefore......
before doAround......
Service2 买
doAfter......
返回的值是:null
doAfterReturning+Obj......
doAround......
after doAround......