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> ...
随机推荐
- -bash: fork: Cannot allocate memory
今天遇到服务器无法SSH,VNC操作命令提示fork:cannot allocate memory free查看内存还有(注意,命令可能要多敲几次才会出来) 查看最大进程数 sysctl kernel ...
- Flink – CEP NFA
看看Flink cep如何将pattern转换为NFA? 当来了一条event,如果在NFA中执行的? 前面的链路,CEP –> PatternStream –> select –> ...
- dbgrideh添加列、多表头及属性
(一)动态添加列 procedure TForm2.FormCreate(Sender: TObject); var vCol : TColumn; begin vCol := DBGrid1 ...
- http请求的基本介绍
响应码 1xx:接收到请求并且继续处理 ,这个是一瞬间的状态,一般不关注 2xx:请求已被接收,理解,处理,表示正确 3xx:请求重定向 4xx:客户请求语法错误或者请求资源不存在,这个是客户端错误 ...
- selenium+xpath在不同层级的写法
总结:定位虽然用Inndex定位最快,但是定位最好不要用浏览器自带定位xpath,尽量不要用Index,否则写的UI自动化脚本的定位元素,需要重新维护.代价太大. 一:不在同一层级,可以用[./..] ...
- jq优化
1.使用链式写法 $('div').find('h3').eq(2).html('Hello');采用链式写法时,jQuery自动缓存每一步的结果,因此比非链式写法要快.根据测试,链式写法比(不使用缓 ...
- dba工作内容
一.数据库管理员的工作内容 关键词:dba工作内容 转自:http://blog.sina.com.cn/s/blog_44e0d0490102won1.html 1.规划与建设: 1.数据库服务器环 ...
- Linux 抓包工具:tcpdump
tcpdump 是一个抓包工具,通常用来分析网络 安装tcpdump命令 [root@mysql test]# yum install -y tcpdump -i 指定网卡 捉取网卡数据包 抓取指定网 ...
- 前端 HTML标签属性
HTML标签可以设置属性,如下: <div id="i1">这是一个div标签</div> <p class='p1 p2 p3'>这是一个段落 ...
- vs 2017 集成python
官网:https://docs.microsoft.com/en-us/visualstudio/python/installation