企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[上一篇博客](http://blog.csdn.net/gaopeng0071/article/details/47106039),我们学习了基于注解配置AOP。 下面我们基于XML来配置AOP。 看代码 ~~~ public interface Calculation { public int add(int x, int y); public int sub(int x, int y); public int mul(int x, int y); public int dev(int x, int y); } ~~~ ~~~ public class CalculationImpl implements Calculation { @Override public int add(int x, int y) { int result = x + y; System.out.println("executeing ..."); return result; } @Override public int sub(int x, int y) { int result = x - y; return result; } @Override public int mul(int x, int y) { int result = x * y; return result; } @Override public int dev(int x, int y) { int result = x / y; System.out.println("executeing ..."); return result; } } ~~~ ~~~ public class CalculationAspect { public void afterReturnMethod(JoinPoint joinPoint, Object ret) { String name = joinPoint.getSignature().getName(); List<Object> list = Arrays.asList(joinPoint.getArgs()); System.out.println("@AfterReturning ... ,method=" + name + ", args = " + list + ", return = " + ret); } public Object aroundMethod(ProceedingJoinPoint pjd) { String name = pjd.getSignature().getName(); List<Object> list = Arrays.asList(pjd.getArgs()); Object obj = null; System.out.println("前置通知 ... ,method=" + name + ", args = " + list); try { obj = pjd.proceed(); System.out.println("返回通知 ... ,method=" + name + ", args = " + list); } catch (Throwable e) { System.out.println("异常通知 ... , exception = " + e); e.printStackTrace(); } System.out.println("后置通知 ... ,method=" + name + ", args = " + list); return obj; } } ~~~ 第一个切入方法,我们其中有后置返回通知、环绕通知两个方法。 ~~~ public class ValidateAspect { public void validate(){ System.out.println("验证方法 com.gp.spring.aop.impl.ValidateAspect"); } } ~~~ 第二个切入方法。 ~~~ public class ValidateAspect { public void validate(){ System.out.println("验证方法 com.gp.spring.aop.impl.ValidateAspect"); } } ~~~ ~~~ <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" xmlns:cache="http://www.springframework.org/schema/cache" 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-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd"> <bean id="calculationImpl" class="com.gp.spring.aop.impl.xml.CalculationImpl"></bean> <bean id="calculationAspect" class="com.gp.spring.aop.impl.xml.CalculationAspect"></bean> <bean id="validateAspect" class="com.gp.spring.aop.impl.xml.ValidateAspect"></bean> <aop:config> <aop:pointcut expression="execution(public int com.gp.spring.aop.impl.xml.Calculation.*(int, int))" id="pointcut" /> <aop:aspect ref="calculationAspect" order="1"> <aop:after-returning method="afterReturnMethod" pointcut-ref="pointcut" returning="ret" /> <aop:around method="aroundMethod" pointcut-ref="pointcut" /> </aop:aspect> <aop:aspect ref="validateAspect" order="2"> <aop:before method="validate" pointcut-ref="pointcut" /> </aop:aspect> </aop:config> </beans> ~~~ 核心的东西来了 这里我们用XML配置代理了注解 ![这里写图片描述](https://box.kancloud.cn/2016-03-15_56e7afc1c7d4e.jpg "") 执行测试方法 ~~~ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml"); Calculation calculation = (Calculation)context.getBean("calculationImpl"); int result = calculation.add(1, 3); System.out.println(result); } ~~~ 输出结果 ~~~ 前置通知 … ,method=add, args = [1, 3] 验证方法 com.gp.spring.aop.impl.ValidateAspect executeing … @AfterReturning … ,method=add, args = [1, 3], return = 4 返回通知 … ,method=add, args = [1, 3] 后置通知 … ,method=add, args = [1, 3] 4 ~~~