aop作用:为实现代码的复用 故使用SpringAOP
一、创建项目
根据项目需求引包:
application.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.jd"></context:component-scan>
<!-- autoproxy自动注入:自动给匹配execution表达式的目标类生成代理类并创建代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
aspectj-autoproxy执行过程 ①Spring寻找含有@Aspect注解的类;
②接着寻找该类中的所有方法;
③从而获取到该类中所有方法的注解;
④然后获取注解中的表达式(execution(int mul(int , int ));
⑤然后再此检查Spring能扫描到的所有类,找到与表达式能够匹配的方法所对应的类;
⑥最后为匹配到的类创建动态对象。
勾选aop,beans,context
二、详细说明
测试类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
//前置增强:在目标方法之前执行
@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]+"]");
}
//后置增强:在目标方法执行之后执行,无论目标方法运行期间是否出现异常。(后置增强无法获取目标方法执行结果,可以在返回增强中获取)
@After(value="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= "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= "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= "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;
}