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