Spring AOP两种实现方法:基于注解与基于xml配置文件
Spring AOP简介:
Spring AOP 即面向切面编程,它是面向对象编程oop的一种扩充,可以使用它完成一些通知,日志或者验证等额外需求而不用去修改源代码,这样能够大大提高源代码的整洁性和可读性,因为AOP能让源代码只剩下核心的业务逻辑,在后期的维护也是大有益处。
Spring AOP的五种通知类型:
- 前置通知(Before):方法执行前执行
- 返回通知(AfterRunning):方法返回时执行(若出现异常可以没有方法没有返回则不会执行)
- 异常通知(AfterThrowing):执行方法时发生异常时执行
- 后置通知(After):方法执行后通知(无论是否发生异常)
- 环绕通知(Around):围绕着方法执行,以下整个动态代理就可以看成环绕通知
Spring AOP原理实现之动态代理:原理实现之动态代理
SpringAOP基于注解实现
所需jar下载链接:jar包免费下载
Spring核心配置文件applicationContext.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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--自动扫描-->
<context:component-scan base-package="com.ldx.aop.annotion"></context:component-scan>
<!-- 开启自动生成代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
接口类Calculator:
package com.ldx.aop.annotion;
public interface Calculator {
public int add(int i, int j);
public int sub(int i, int j);
public int mul(int i, int j);
public int div(int i, int j);
}
接口实现类CalculatorImpl:
package com.ldx.aop.annotion;
import org.springframework.stereotype.Component;
@Component
public class CalculatorImpl implements Calculator {
@Override
public int add(int i, int j) {
return i + j;
}
@Override
public int sub(int i, int j) {
return i - j;
}
@Override
public int mul(int i, int j) {
return i * j;
}
@Override
public int div(int i, int j) {
return i / j;
}
}
切面类LogAspec:
package com.ldx.aop.annotion;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@Aspect
public class LogAspec {
//定义一个切点表达式,可以重用,不用在每个方法上写一长串,引用方法名即可
@Pointcut("execution(* com.ldx.aop.annotion.Calculator.*(..))")
public void myPointcut() {
}
@Before("myPointcut()")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("前置通知:the method " + methodName + " begin with args " + Arrays.asList(args));
}
@After("myPointcut()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("后置通知:the method " + methodName + " end");
}
@AfterReturning(value = "myPointcut()", returning = "result")
public void returnMethod(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("返回通知:the method " + methodName + " result=" + result);
}
@AfterThrowing(value = "myPointcut()", throwing = "e")
public void throwMethod(JoinPoint joinPoint, Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("异常通知:the method " + methodName + " cause exception:" + e);
}
}
测试类Test:
package com.ldx.aop.annotion;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Calculator calculator = (Calculator) ac.getBean("calculatorImpl");
System.out.println("---------加法---------");
int result = calculator.add(1, 3);
System.out.println("-->加法结果:"+result);
System.out.println("---------减法---------");
result = calculator.sub(1, 3);
System.out.println("-->减法结果:"+result);
System.out.println("---------乘法---------");
result = calculator.mul(1, 3);
System.out.println("-->乘法结果:"+result);
System.out.println("---------无异常除法---------");
result = calculator.div(1, 3);
System.out.println("-->除法结果:"+result);
System.out.println("---------异常除法---------");
result = calculator.div(1, 0);
System.out.println("-->除法结果:"+result);
}
}
运行结果:
从结果可以看出,当发生异常时,返回通知不会执行。
Spring AOP基于xml配置文件实现
Spring核心配置文件applicationContext-xml.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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="calculator"
class="com.ldx.aop.xml.CalculatorImpl"></bean>
<bean id="logAspec2"
class="com.ldx.aop.xml.LogAspec"></bean>
<aop:config>
<aop:aspect id="aspect" ref="logAspec2">
<aop:pointcut id="mypointcut" expression="execution(* com.ldx.aop.xml.Calculator.*(..))"></aop:pointcut>
<aop:before method="beforeMethod" pointcut-ref="mypointcut"></aop:before>
<aop:after method="afterMethod" pointcut-ref="mypointcut"></aop:after>
<aop:after-returning method="returnMethod" pointcut-ref="mypointcut" returning="result"></aop:after-returning>
<aop:after-throwing method="throwMethod" pointcut-ref="mypointcut" throwing="e"></aop:after-throwing>
</aop:aspect>
</aop:config>
</beans>
接口类Calculator:
package com.ldx.aop.xml;
public interface Calculator {
public int add(int i, int j);
public int sub(int i, int j);
public int mul(int i, int j);
public int div(int i, int j);
}
接口实现类CalculatorImpl:
package com.ldx.aop.xml;
import org.springframework.stereotype.Component;
@Component
public class CalculatorImpl implements Calculator {
@Override
public int add(int i, int j) {
return i + j;
}
@Override
public int sub(int i, int j) {
return i - j;
}
@Override
public int mul(int i, int j) {
return i * j;
}
@Override
public int div(int i, int j) {
return i / j;
}
}
切面类LogAspec:
package com.ldx.aop.xml;
import org.aspectj.lang.JoinPoint;
import java.util.Arrays;
public class LogAspec {
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("前置通知:the method " + methodName + " begin with args " + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("后置通知:the method " + methodName + " end");
}
public void returnMethod(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("返回通知:the method " + methodName + " result=" + result);
}
public void throwMethod(JoinPoint joinPoint, Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("异常通知:the method " + methodName + " cause exception:" + e);
}
}
测试类Test:
package com.ldx.aop.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
Calculator calculator = (Calculator) applicationContext.getBean("calculator");
System.out.println("---------加法---------");
int result = calculator.add(1, 3);
System.out.println("-->加法结果:" + result);
System.out.println("---------减法---------");
result = calculator.sub(1, 3);
System.out.println("-->减法结果:" + result);
System.out.println("---------乘法---------");
result = calculator.mul(1, 3);
System.out.println("-->乘法结果:" + result);
System.out.println("---------无异常除法---------");
result = calculator.div(1, 3);
System.out.println("-->除法结果:" + result);
System.out.println("---------异常除法---------");
result = calculator.div(1, 0);
System.out.println("-->除法结果:" + result);
}
}
运行结果:
结果与基于注解的实现一致。