aop编程之前置通知
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. 运行结果
aop编程之前置通知的更多相关文章
- aop编程之后置通知,环绕通知和异常通知
---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html 具体流程如下: 1. ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- 12Spring_AOP编程(AspectJ)_前置通知
接下里的博客会一篇一篇的讲解每一个通知.其实AOP_AspectJ的编程与传统的AOP的编程的最大的区别就是写一个Aspect 支持多个Advice和多个PointCut .而且我们写AOP_Aspc ...
- Spring(三)--AOP【面向切面编程】、通知类型及使用、切入点表达式
1.概念:Aspect Oriented Programming 面向切面编程 在方法的前后添加方法 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动 ...
- AOP 和 前置通知,后置通知
Spring 1.AOP:中文名称面向切面编程 2.英文名称:(Aspect Oriented Programming) 3.正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执行 ...
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- Spring AOP 前置通知
我们使用AspectJ对Spring进行AOP操作,有两种方式,注解和XML配置方式,先在pom.xml中声明jar包 <dependencies> <dependency> ...
随机推荐
- 交叉编译qxmpp cmake格式工程
编写Toolchain-aarch64.cmake文件,内容如下: # this is required SET(CMAKE_SYSTEM_NAME Linux) # 必须 set(CMAKE_SYS ...
- inotifywait实现目录监控--http://man.linuxde.net/inotifywait
sudo apt install inotify-tools while inotifywait -q -r -e create,delete,modify,move,attrib --exclude ...
- dyld环境变量
苹果APP启动,分为两个过程:系统dylib动态链接库 app的main函数启动过程. main函数过程直接对iOS开发者.这里备忘的dylib过程: 一.dyld加载到虚拟内存 1. loa ...
- take a cpu core offline
[root@vrouter1 ~]# cat /sys/devices/system/cpu/online -,,- [root@vrouter1 ~]# cat /sys/devices/syste ...
- [archlinux][daily] 自建DNS服务器 / 建立本地DNS cache / 使用dnsmasq加速上网
新公司,上网超慢,DNS竟然是远程地址,终于找到机会学习一下dnsmasq了. update@20170516: 上网慢是因为分给我的IP有限流策略,其实远端DNS并不会造成感受上的上网慢. 参考:h ...
- AndroidStudio_Button
这里回顾一下Button的使用方法: 1.在page1.xml文件中定义一个按钮控件 <Button android:id="@+id/btn_textview" andro ...
- odoo 权限设置
*权限管理的四个层次 # 菜单级别:不属于指定菜单所包含组的用户看不到该菜单,不客全,只是隐藏 菜单,若知道菜单ID,仍然可以通过指定URL访问 # 对象级 ...
- 写一致性原理以及quorum机制
(1)consistency,one(primary shard),all(all shard),quorum(default)我们在发送任何一个增删改操作的时候,比如 PUT /index/type ...
- 【托业】【新托业TOEIC新题型真题】学习笔记2-题库一-->P5-6
P5-6 --------------------------------------单词-------------------------------------- transfrom 转化 jus ...
- 字符集更改步骤,mysql乱码
关键字:Mysql乱码,mysql字符集修改 #字符集更改步骤~