赞
踩
导入aop的maven依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
创建一个spring的XML文件编写aop配置
<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 装配切面类 --> <bean id="mateDataServiceAspect" class="com.example.testproject.demo.aspect.MateDataServiceAspect"/> <!--可以配置多个 aop:config 用于监听多个切面--> <aop:config> <aop:pointcut id="myExecution" expression="execution(* com.example.testproject.demo.controller.DemoController.test2(..))"/> <aop:aspect ref="mateDataServiceAspect"> <aop:before method="before" pointcut-ref="myExecution"/> <aop:after-returning method="afterReturning" pointcut-ref="myExecution" returning="methodResult"/> </aop:aspect> </aop:config> </beans>
编写切面代码
public class MateDataServiceAspect { public void before() { System.out.println(">>>>>>>目录aop方法开始执行>>>>>>>>"); } /** * 方法执行完后调用 * @param joinPoint * @param methodResult */ public void afterReturning(JoinPoint joinPoint, Object methodResult){ // 方法执行完返回的结果 System.out.println("监听方法的结果 " + methodResult); // 获取方法的入参 Object[] args = joinPoint.getArgs(); List<Object> objects = Arrays.asList(args); System.out.println("传入的参数:" + objects); } }
被监听的方法
调通接口测试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。