spring aop
pom.xml
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency>
Hello
package org.mythsky.springaopdemo; public interface Hello { // 定义一个简单方法,模拟应用中的业务逻辑方法 void foo(); // 定义一个addUser()方法,模拟应用中的添加用户的方法 int addUser(String name , String pass); }
HelloImpl
package org.mythsky.springaopdemo; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; @Component("hello") public class HelloImpl implements Hello { public void foo() { System.out.println("执行Hello组件的foo()方法"); } public int addUser(String name, String pass) { System.out.println("执行Hello组件的addUser添加用户:"+name); return 0; } }
AuthAspect
package org.mythsky.springaopdemo; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class AuthAspect { @Before("execution(* org.mythsky.springaopdemo.*.*(..))") public void authority(){ System.out.println("模拟执行权限检查"); } }
services.xml
<context:component-scan base-package="org.mythsky.springaopdemo"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
BeanTest
package org.mythsky.springaopdemo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml"); Hello hello = ctx.getBean("hello" , Hello.class); hello.foo(); hello.addUser("孙悟空" , "7788"); } }
运行结果:
这里要注意切面类也需要添加@Component注解。
AfterReturning
LogAspect
package org.mythsky.springaopdemo; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class LogAspect { @AfterReturning(returning = "rvt",pointcut = "execution(* org.mythsky.springaopdemo.*.*(..))") public void log(Object rvt){ System.out.println("获取目标方法返回值:"+rvt); System.out.println("模拟记录日志功能..."); } }
重新运行结果:
AfterThrowing
RepairAspect
package org.mythsky.springaopdemo; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class RepairAspect { @AfterThrowing(throwing = "ex",pointcut = "execution(* org.mythsky.springaopdemo.*.*(..))") public void doRecoveryActions(Throwable ex){ System.out.println("目标方法中抛出的异常:"+ex); System.out.println("模拟Advice对异常的修复..."); } }
修改下HelloImpl
public int addUser(String name, String pass) { System.out.println("执行Hello组件的addUser添加用户:"+name); if(name.length()<3||name.length()>10){ throw new IllegalArgumentException("name参数的长度必须大于3,小于10!"); } return 18; }
修改主程序BeanTest
public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml"); Hello hello = ctx.getBean("hello" , Hello.class); hello.foo(); hello.addUser("悟空" , "7788"); }
运行结果:
After
ReleaseAspect
package org.mythsky.springaopdemo; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class ReleaseAspect { @After("execution(* org.mythsky.springaopdemo.*.*(..))") public void release(){ System.out.println("模拟方法结束后的释放资源..."); } }
运行结果:
Around
TxAspect
package org.mythsky.springaopdemo; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class TxAspect { @Around("execution(* org.mythsky.springaopdemo.*.*(..))") public Object processTx(ProceedingJoinPoint jp) throws Throwable { System.out.println("执行目标方法之前,模拟开始事务..."); Object[] args=jp.getArgs(); if(args!=null&&args.length>1){ args[0]="【增加的前缀】"+args[0]; } Object rvt=jp.proceed(args); System.out.println("执行目标方法之后,模拟结束事务..."); if(rvt!=null&&rvt instanceof Integer) rvt=(Integer)rvt*(Integer)rvt; return rvt; } }
运行结果: