一、spring aop介绍
spring aop可以方便地将业务功能与非业务功能分离,大大降低了程序之间的耦合性;同时它也为系统功能升级提供便利,因为我们可以不触碰原有功能模块,实现现有功能与原有功能的连接。spring通过动态代理实现aop,在高版本中,框架可自动根据当前被代理类选择使用基于接口的动态代理还是基于子类(类)的动态代理;也可手动指定动态代理的方式。
在java中,所有的公有方法(函数)都可以作为连接点(join point),切点(pointcut)就是一个执行通知(advice)的条件,通常使用一个空体方法来声明(在注解中),切面(Aspect)是一个类,它是由pointcut和advice组成;
二、aop注解
@Aspect: 声明一个切面类,放在类上
@Before 增强(通知)方法,在执行pointcut之前运行
@After 增强(通知)方法,在执行pointcut之后运行
@AfterReturning 增强(通知)方法,在执行pointcut后返回结果执行
@AfterThrowing 增强(通知)方法,在执行pointcut时出现异常时执行
@Around: 增强(通知)方法,在执行pointcut之前和之后执行
@Pointcut 定义通知执行的条件(切点表达式)
三、aop注解使用
需求: 我们需要在核心业务类Bussines中进行非核心业务日志开发
核心业务类
@Component("business")
public class Business {
//add
public void add() {
System.out.println("add method!");
}
}
非核心业务类
@Component("logginAspectJ")
@Aspect
public class LogginAspectJ {
@Pointcut("execution(* cn.hidm.aop.service.Business.*(..))")
public void PointcutDeclaration() {}
@Before("PointcutDeclaration()") //通知中的value值是声明切点的方法
public void BeforeMethod(JoinPoint jp) {
String methodName = jp.getSignature().getName();
Object[] args = jp.getArgs();
System.out.println("BeforeMethod The method "+ methodName +" parameter is "+ Arrays.asList(args));
System.out.println("add before");
System.out.println();
}
@After("PointcutDeclaration()")
public void AfterMethod(JoinPoint jp) {
String methodName = jp.getSignature().getName();
Object[] args = jp.getArgs();
System.out.println("AfterMethod The method "+ methodName +" parameter is "+Arrays.asList(args));
System.out.println();
}
@AfterReturning(value="PointcutDeclaration()",returning="result")
public void AfterReturningMethod(JoinPoint jp,Object result) {
String methodName = jp.getSignature().getName();
Object[] args = jp.getArgs();
System.out.println("AfterReturningMethod The method "+ methodName +" parameter is "+Arrays.asList(args)+" "+result);
System.out.println();
}
@AfterThrowing(value="PointcutDeclaration()",throwing="e")
public void AfterThrowingMethod(JoinPoint jp,Exception e) {
String methodName = jp.getSignature().getName();
System.out.println("AfterThrowingMethod The method "+ methodName +"exception :"+e);
}
}
测试类
public class AopTest {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Business arithmetic = (Business) context.getBean("business");
arithmetic.add();
context.close();
}
}
四、aop的xml使用
原业务类
public class FunctionMethod {
public FunctionMethod() {
super();
}
public void method1() {
System.out.println("业务方法1");
}
}
切面类
public class MyAdvice {
public MyAdvice() {
super();
}
public void adviceBefore() {
System.out.println("前通知运行了");
}
public void adviceAfter() {
System.out.println("后通知运行了");
}
}
xml配置
<aop:config>
<!-- 配置切入点 expression填写切入点表达式 -->
<aop:pointcut
expression="execution(* cn.hidm.aopxml.service.FunctionMethod.method1(..))"
id="pointcut" />
<!-- 配置切面 切面是切入点和通知的结合 -->
<aop:aspect ref="myAdvise">
<!-- 指定名为before方法作为前置通知 -->
<aop:before method="adviceBefore" pointcut-ref="pointcut" />
<aop:around method="adviceAfter" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
测试类
@Test
public void test1() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
FunctionMethod functionMethod = (FunctionMethod)ac.getBean("functionMethod");
functionMethod.method1();
ac.close();
}