Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around

aop作用:为实现代码的复用 故使用SpringAOP 一、创建项目 Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around
文章图片
Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around
文章图片
根据项目需求引包:
application.xml


aspectj-autoproxy执行过程①Spring寻找含有@Aspect注解的类;
②接着寻找该类中的所有方法;
③从而获取到该类中所有方法的注解;
④然后获取注解中的表达式(execution(int mul(int , int ));
⑤然后再此检查Spring能扫描到的所有类,找到与表达式能够匹配的方法所对应的类;
⑥最后为匹配到的类创建动态对象。
勾选aop,beans,context
Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around
文章图片

Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around
文章图片

二、详细说明
测试类Test.java
package com.jd.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jd.computer.service.IComputerService; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml"); IComputerService computerService = applicationContext.getBean(IComputerService.class); System.out.println(computerService.getClass().getName()); int result = computerService.div(1, 1); System.out.println(result); applicationContext.close(); }}

CumputerAOP.java
Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around
文章图片

//前置增强:在目标方法之前执行 @Before("execution(public int com.jd.computer.service.ComputerService.*(..))") public void beford(JoinPoint jp) { Object [] agrs = jp.getArgs(); //获取列表的参数 Signature signature = jp.getSignature(); //获取名字 String name = signature.getName(); System.out.println(this.getClass().getName()+":The "+name+" method begins."); System.out.println(this.getClass().getName()+":Parameters of the div method: ["+agrs[0]+","+agrs[1]+"]"); }

Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around
文章图片

//后置增强:在目标方法执行之后执行,无论目标方法运行期间是否出现异常。(后置增强无法获取目标方法执行结果,可以在返回增强中获取) @After(value="https://www.it610.com/article/execution(public int com.jd.cumputer.service.ComputerService.*(..))") public void after(JoinPoint jp) { Signature signature = jp.getSignature(); String name = signature.getName(); System.out.println(this.getClass().getName()+":The "+name+" method ends."); }


@AfterReturning(value= "https://www.it610.com/article/execution(public int com.jd.cumputer.service.ComputerService.*(..))" ,returning="result") public void afterReturning(JoinPoint jp,Object result) { Signature signature = jp.getSignature(); String name = signature.getName(); System.out.println(this.getClass().getName()+":Result of the "+name+" method :" + result); } @AfterThrowing(value= "https://www.it610.com/article/execution(public int com.jd.cumputer.service.ComputerService.*(..))" ,throwing="e") public void afterThrowing(JoinPoint jp,Exception e) { System.out.println(e.getMessage()); }


1.@Before对应于方法开始执行之前;@After对应于方法结束;@AfterThrowing对应于抛出异常后;@AfterReturning在出现异常时不执行。
2.@AfterReturning是在返回成功时调用;@After是在方法结束之后、执行return之前调用

@Around 可代替前面四种
@Around(value= "https://www.it610.com/article/execution(public int com.jd.cumputer.service.ComputerService.*(..))") public Object around(ProceedingJoinPoint pjp) { Object [] agrs = pjp.getArgs(); Signature signature =pjp.getSignature(); String name = signature.getName(); System.out.println(this.getClass().getName()+":The "+name+" method begins."); System.out.println(this.getClass().getName()+":Parameters of the div method: ["+agrs[0]+","+agrs[1]+"]"); try { Object result =null; try { Object object = pjp.getTarget(); result=pjp.proceed(); }finally { System.out.println(this.getClass().getName()+":The "+name+" method ends."); } System.out.println(this.getClass().getName()+":Result of the "+name+" method :" + result); return result; }catch(Throwable e){ System.out.println(e.getMessage()); } return -1; }

【Spring|Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around】

    推荐阅读