AOP 基于 XML 的配置声明切面
- 其他文件
- ArithmeticCalculator.java(com.test.spring.aop.xml.ArithmeticCalculator)
- ArithmeticCalculatorImpl.java(com.test.spring.aop.xml.ArithmeticCalculatorImpl)
- LoggingAspect.java(com.test.spring.aop.xml.LoggingAspect)
- VlidationAspect.java(com.test.spring.aop.xml.VlidationAspect)
- Main.java(com.test.spring.aop.xml.Main)
记得一点 前置通知&后置通知&异常通知&返回通知和环绕通知不要一块使用,
因为环绕通知就是他们全部,最牛逼的,也是最不常用的
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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置 bean -->
<bean id="arithmeticCalculator" class="com.test.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
<!-- 配置切面的 bean -->
<bean id="loggingAspect" class="com.test.spring.aop.xml.LoggingAspect"></bean>
<bean id="vlidationAspect" class="com.test.spring.aop.xml.VlidationAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(* com.test.spring.aop.xml.ArithmeticCalculator.*(int, int))"></aop:pointcut>
<!-- 配置切面及通知 -->
<!-- 使用 order 配置切入点的优先级 -->
<aop:aspect ref="loggingAspect" order="2">
<!-- 前置通知 -->
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
<!-- 后置通知 -->
<aop:after method="afterMethod" pointcut-ref="pointcut"></aop:after>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing>
<!-- 返回通知 -->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"></aop:after-returning>
<!-- 环绕通知 -->
<aop:around method="aroudMethod" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
<!-- 使用 order 配置切入点的优先级 -->
<aop:aspect ref="vlidationAspect" order="1">
<!-- 前置通知 -->
<aop:before method="validateArgs" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
</beans>
其他文件
ArithmeticCalculator.java(com.test.spring.aop.xml.ArithmeticCalculator)
package com.test.spring.aop.xml;
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
ArithmeticCalculatorImpl.java(com.test.spring.aop.xml.ArithmeticCalculatorImpl)
package com.test.spring.aop.xml;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
LoggingAspect.java(com.test.spring.aop.xml.LoggingAspect)
package com.test.spring.aop.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import java.util.Arrays;
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs exception " + ex);
}
@Around("execution(public int com.test.spring.aop.ArithmeticCalculator.*(..))")
public Object aroudMethod(ProceedingJoinPoint pjd) {
Object result = null;
String methodName = pjd.getSignature().getName();
// 执行目标方法
try {
// 前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
// 执行目标方法
result = pjd.proceed();
// 返回通知
System.out.println("The method ends with " + result);
} catch (Throwable e) {
// 异常通知
System.out.println("The method " + methodName + " occurs exceptio:" + e);
throw new RuntimeException(e);
}
// 后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}
VlidationAspect.java(com.test.spring.aop.xml.VlidationAspect)
package com.test.spring.aop.xml;
import org.aspectj.lang.JoinPoint;
import java.util.Arrays;
public class VlidationAspect {
public void validateArgs(JoinPoint joinPoint){
System.out.println("--> validate:" + Arrays.asList(joinPoint.getArgs()));
}
}
Main.java(com.test.spring.aop.xml.Main)
package com.test.spring.aop.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
System.out.println(arithmeticCalculator.getClass().getName());
int result = arithmeticCalculator.add(1,2);
System.out.println("result:" + result);
result = arithmeticCalculator.div(1000,10);
System.out.println("result:" + result);
}
}