aop( Aspect-Oriented Programming)前置通知原理案例讲解

编程步骤;

  1. 定义接口
  2. 编写对象(被代理的对象即目标对象)
  3. 编写通知(前置通知即目标方法调用前调用)
  4. 在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编程之前置通知的更多相关文章

  1. aop编程之后置通知,环绕通知和异常通知

    ---恢复内容开始--- 此将实例将在上一讲前置通知的基础上进行配置,前置配置内容:http://www.cnblogs.com/lihuibin/p/7955947.html  具体流程如下: 1. ...

  2. Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知

    本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知     1. Spring AOP  前置通知 XML配置使用案例     2. Spring AOP   ...

  3. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  4. 12Spring_AOP编程(AspectJ)_前置通知

    接下里的博客会一篇一篇的讲解每一个通知.其实AOP_AspectJ的编程与传统的AOP的编程的最大的区别就是写一个Aspect 支持多个Advice和多个PointCut .而且我们写AOP_Aspc ...

  5. Spring(三)--AOP【面向切面编程】、通知类型及使用、切入点表达式

    1.概念:Aspect Oriented Programming 面向切面编程 在方法的前后添加方法   2.作用:本质上来说是一种简化代码的方式      继承机制      封装方法      动 ...

  6. AOP 和 前置通知,后置通知

    Spring 1.AOP:中文名称面向切面编程 2.英文名称:(Aspect Oriented Programming) 3.正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执行 ...

  7. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

  9. Spring AOP 前置通知

    我们使用AspectJ对Spring进行AOP操作,有两种方式,注解和XML配置方式,先在pom.xml中声明jar包 <dependencies> <dependency> ...

随机推荐

  1. day5_递归调用

    #递归的意思,函数自己调用自己#递归最多递归999次#递归的效率没有循环高 实例1-递归调用: count = 0 def say(): global count count += 1 print(' ...

  2. LeetCode 509 Fibonacci Number 解题报告

    题目要求 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, su ...

  3. MySQL数据库改名的三种方法

    前不久去面试,被问到Innodb引擎的表如何改数据库名,当时我也只回答了MyISAM改如何操作,被一些细节问题打败,真是操蛋. 如果表示MyISAM那么可以直接去到数据库目录mv就可以. Innodb ...

  4. (4.6)mysql备份还原——深入解析二进制日志(2)binlog参数配置解析

    关键词:binlog配置,binlog参数,二进制日志配置,二进制文件参数配置 关键词:binlog缓存,binlog 刷新 0.bin写入流程 写binlog流程如下:# 数据操作buffer po ...

  5. SQL数据库中临时表、临时变量和WITH AS关键词创建“临时表”的区别

    原文链接:https://www.cnblogs.com/zhaowei303/articles/4204805.html SQL数据库中数据处理时,有时候需要建立临时表,将查询后的结果集放到临时表中 ...

  6. 永久有效的 webstorm license server 20180808

    下载地址  https://download.jetbrains.com/webstorm/WebStorm-2018.3.2.exe 2018年10月26日,最近老是过期,搞了一个1年有效的代码,是 ...

  7. 怎样打开U盘最安全

    为了避免电脑使用U盘时,通过双击,或者右击盘符时,导致把病毒感染至整个电脑,因此使用下面的方法,可使U盘病毒不被激活传播. 在取消了U盘自动运行的情况下(在组策略中一定要关闭自动运行功能,否则只要一插 ...

  8. 用PE系统安装原版XP

    方法:直接运行Winnt32程序进行XP原版系统安装.    [1].在PE系统中将XP SP3系统镜像ISO文件从U盘上复制到硬盘的非系统分区后,用PE所带WinRAR程序将该ISO镜像中的I386 ...

  9. php5.6+Redis+Windows7安装 (phpstudy)

    Windows下为PHP安装redis扩展 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本. 2.下载php_igbinary-2.0.1-7.0-ts-vc14-x64.z ...

  10. vux 是基于 WeUI 和Vue(2.x)开发的移动端UI组件库,主要服务于微信页面。

    https://doc.vux.li/zh-CN/ https://vux.li/