1.创建相应的类

2.代码

service沿用前面的

增加两个log

Log.java

package com.shao.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
//Method:受执行的目标对象的方法
//objects: 参数
//o:目标对象
public void before(Method method, Object[] objects, Object o) throws Throwable {
assert o != null;
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
}

AfterLog.java

package com.shao.log;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class AfterLog implements AfterReturningAdvice { //执行之后拿回了返回值
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+", 返回了"+o);
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="userService" class="com.shao.service.UserServiceImpl"/>
<bean id="log" class="com.shao.log.Log"/>
<bean id="afterLog" class="com.shao.log.AfterLog"/> <!--配置Aop:导入AOP约束-->
<aop:config>
<!--切入点:expression表达式:要执行的位置-->
<!--这个类的所有方法-->
<aop:pointcut id="pointcut" expression="execution(* com.shao.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<!--把log这个类切入到这个方法中去-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>

3.测试

import com.shao.service.UserService;
import com.shao.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
@org.junit.Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//一定要注意:动态代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

最新文章

  1. Apache Shiro系列之五,概述 —— 配置
  2. linux系统的任务计划crontab使用详解
  3. 17243 Huzi酱和他的俄罗斯套娃(贪心)
  4. 【你吐吧c#每日学习】10.29 C#字符串类型&amp;Common operators
  5. new 、operator new 和 placement new
  6. SVN 外部引用(svn:externals)处理相似系统的公用代码
  7. 《Thinking in C++》学习笔记(二)【第三章】
  8. bzoj 3218 a + b Problem(最小割+主席树)
  9. PL/SQL学习(六)触发器
  10. hadoop启动守护进程报JAVA_HOME is not set and could not be found
  11. IController控制器的创建过程
  12. shell-逐行读取文件
  13. jquery 中时间的运用
  14. 如何去掉word中的回车符??
  15. CSS中清除浮动的作用以及如何清除浮动
  16. MySQL系列详解一:MySQL&amp;&amp;多实例安装-技术流ken
  17. Python全栈-day1-day2-计算机基础
  18. Go第十一篇之编译与工具
  19. spring data jpa分页5种方法
  20. Controller层@PathVariable使用

热门文章

  1. js 判断一个变量是否有值
  2. Visual Studio 安装 C++
  3. Flask 易错点
  4. Java:创建对象小记
  5. [对对子队]会议记录4.11(Scrum Meeting 2)
  6. [no_code]OCR表格处理——功能规格说明书
  7. 带你用AVPlayer实现音频和视频播放
  8. (四)、Docker 镜像
  9. 2021.10.26考试总结[冲刺NOIP模拟16]
  10. Linux有什么可取之处竟如此受欢迎