关键代码:
<!-- 配置AOP --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut id="pointCut" expression="execution(* com.shuoeasy.test.ArithmeticCalculator.*(double, double))" /> <!-- 配置日志类 --> <aop:aspect ref="logAspect" order="2"> <!-- 前置通知 --> <aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before> <!-- 后置通知 --> <aop:after method="alterMethod" pointcut-ref="pointCut"/> <!-- 返回通知 --> <aop:after-returning method="afterReturning" pointcut-ref="pointCut" returning="result"/> <!-- 异常通知 --> <aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="ex"/> <!-- 环绕通知 --> <aop:around method="aroundMethod" pointcut-ref="pointCut"/> </aop:aspect> <!-- 配置验证类 --> <aop:aspect ref="verificationAspect" order="1"> <!-- 前置通知 --> <aop:before method="beforeMethod" pointcut-ref="pointCut"/> </aop:aspect> </aop:config>
详细:
ArithmeticCalculator.java
package com.shuoeasy.test; /** * 定义计算器接口 * */ public interface ArithmeticCalculator { double add(double num1, double num2); double sub(double num1, double num2); double mul(double num1, double num2); double div(int num1, int num2); }
ArithmeticCalculatorImpl.java:
package com.shuoeasy.test; /** * 业务类 */ public class ArithmeticCalculatorImpl implements ArithmeticCalculator{ public double add(double num1, double num2) { double result = num1 + num2; return result; } public double sub(double num1, double num2) { double result = num1 - num2; return result; } public double mul(double num1, double num2) { double result = num1 * num2; return result; } public double div(int num1, int num2) { double result = num1 / num2; return result; } }
LogAspect.java:
package com.shuoeasy.test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class LogAspect { public void beforeMethod(JoinPoint joinPoint){ System.out.println("日志-前置通知"); } public void alterMethod(JoinPoint joinPoint){ System.out.println("日志-后置通知"); } public void afterReturning(JoinPoint joinPoint, Object result){ System.out.println("日志-返回通知"); } public void afterThrowing(JoinPoint joinPoint, Exception ex){ System.out.println("日志-异常通知"); } public Object aroundMethod(ProceedingJoinPoint pjd){ System.out.println("日志-环绕通知"); return 100.0; } }
VerificationAspect.java:
package com.shuoeasy.test; import org.aspectj.lang.JoinPoint; // 验证类 public class VerificationAspect { public void beforeMethod(JoinPoint joinPoint){ System.out.println("验证-前置通知"); } }
bean.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:p="http://www.springframework.org/schema/p" 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.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="arithmeticCalculator" class="com.shuoeasy.test.ArithmeticCalculatorImpl"></bean> <bean id="logAspect" class="com.shuoeasy.test.LogAspect"></bean> <bean id="verificationAspect" class="com.shuoeasy.test.VerificationAspect"></bean> <!-- 配置AOP --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut id="pointCut" expression="execution(* com.shuoeasy.test.ArithmeticCalculator.*(double, double))" /> <!-- 配置日志类 --> <aop:aspect ref="logAspect" order="2"> <!-- 前置通知 --> <aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before> <!-- 后置通知 --> <aop:after method="alterMethod" pointcut-ref="pointCut"/> <!-- 返回通知 --> <aop:after-returning method="afterReturning" pointcut-ref="pointCut" returning="result"/> <!-- 异常通知 --> <aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="ex"/> <!-- 环绕通知 --> <aop:around method="aroundMethod" pointcut-ref="pointCut"/> </aop:aspect> <!-- 配置验证类 --> <aop:aspect ref="verificationAspect" order="1"> <!-- 前置通知 --> <aop:before method="beforeMethod" pointcut-ref="pointCut"/> </aop:aspect> </aop:config> </beans>
Main.java:
package com.shuoeasy.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // 1.创建spring的IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); // 2.从IOC容器中获取bean的实例 ArithmeticCalculator arithmeticCalulator = (ArithmeticCalculator)ctx.getBean("arithmeticCalculator"); // 3.使用bean double result = arithmeticCalulator.add(2, 3); System.out.println("result=" + result); } }
输出:
验证-前置通知 日志-前置通知 日志-环绕通知 日志-返回通知 日志-后置通知 result=100.0