aop编程之前置通知
2024-09-12 20:30:05
aop( Aspect-Oriented Programming)前置通知原理案例讲解
编程步骤;
- 定义接口
- 编写对象(被代理的对象即目标对象)
- 编写通知(前置通知即目标方法调用前调用)
- 在beans.xml文件中配置
4.1. 配置 被代理对象即目标对象
4.2. 配置通知
4.3. 配置代理对象 其是ProxyFactoryBean的对象实例
4.3.1 配置代理接口集
4.3.2 织入通知
4.3.3 配置被代理对象
直接上代码
1.分别创建两个接口如下:
TestServiceInterface.java接口
package com.LHB.aop; public interface TestServiceInterface { public void sayHello();
}
TestServiceInterface2.java接口
package com.LHB.aop; public interface TestServiceInterface2 { public void sayBye();
}
2. 创建被代理对象(目标对象)类testService.java,该类实现了TestServiceInterface和TestServiceInterface2两个接口方法
package com.LHB.aop; public class testService implements TestServiceInterface,TestServiceInterface2 { private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub System.out.println("hello " + name);
}
@Override
public void sayBye() {
// TODO Auto-generated method stub
System.out.println("Bye "+ name);
} }
3. 编写前置通知即MyMethodBeforeAdvice.java,该类实现了MethodBeforeAdvice接口中的before(method method,Object[] args,Object target )方法。
注意:在实现该接口的时候需要导入spring-aop-5.0.1.RELEASE.jar包(本案例在spring5.0.1版本下实现的)
package com.LHB.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MyMethodBeforeAdvice implements MethodBeforeAdvice { /**
* 功能:该函数将在目标函数执行前首先执行
* method:被调用方法名字
* args:给method传递的参数
* target:被代理的目标对象
*/
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
//method.getName()方法将得到目标函数的函数名称
System.out.println("记录日志...." + method.getName());
} }
4.通过new/File新建一个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:context="http://www.springframework.org/schema/context"
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.xsd"> <!--4.1 配置一个被代理的对象即目标对象-->
<bean id="testService" class="com.LHB.aop.testService">
<property name="name" value="张三" />
</bean>
<!--4.2 配置前置通知 -->
<bean id="MyMethodBeforeAdvice" class="com.LHB.aop.MyMethodBeforeAdvice" />
<!--4.3 配置代理对象 -->
<bean id="ProxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--4.3.1 配置代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>com.LHB.aop.TestServiceInterface</value>
<value>com.LHB.aop.TestServiceInterface2</value>
</list>
</property>
<!--4.3.2 把通知织入到代理对象 -->
<property name="interceptorNames">
<!-- 相当于把MyMethodBeforeAdvice前置通知把代理对象关联起来,也可以把通知看成拦截器 -->
<value>MyMethodBeforeAdvice</value>
</property>
<!--4.3.3 配置被代理对象可以指定 -->
<property name="target" ref="testService"/> </bean> </beans>
5. 创建一个测试类APP
package com.LHB.aop; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("com/LHB/aop/beans.xml");
TestServiceInterface tsi = (TestServiceInterface) ac.getBean("ProxyFactoryBean");
tsi.sayHello();
((TestServiceInterface2)tsi).sayBye(); } }
6. 运行结果
最新文章
- [BZOJ1501][NOI2005] 智慧珠游戏
- php.ini修改php上传文件大小限制的方法详解
- 十五个常用的jquery代码段
- Scalaz(9)- typeclass:checking instance abiding the laws
- mac 下真机调试 android 手机
- maven核心概念4
- hdu 1412 (STL list)
- bzoj1630 [Usaco2007 Demo]Ant Counting
- Arduino周边模块:LED部件
- Notes系统安全日志
- hexo常用命令笔记
- 关于Android attrs 自定义属性的说明
- 一种dubbo逻辑路由方案
- Haskell学习-monad
- 安装Kali的小问题
- 【NET Core】事务TransactionScope
- C++类的大小——sizeof(class)
- MySQL 5.7.20 ONLY_FULL_GROUP_BY
- 利用Bootstrap+Avalonjs+EntityFramework 开发ASP.NET WebForm应用程序(上)
- 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串
热门文章
- 用CMD命令进行关机/重启
- 【软件测试】Junit入门
- Xcode编译警告Assigning to &#39;id<;XXXDelegat>; ——Nullable&#39; from incompatible type &#39;XXXView *const_strong&#39;
- AWS学习笔记
- LeetCode 804 Unique Morse Code Words 解题报告
- 关于hover的一个问题记录
- 使用docker部署Asp.net core web应用程序
- php 7 新特性整理小结
- RN 获取地理位置
- 015-线程同步-synchronized几种加锁方式、Java对象头和Monitor、Mutex Lock、JDK1.6对synchronized锁的优化实现