spring-xml实现aop-通知的种类
如果本代码有疑问,请访问spring-aop快速入门或者spring-aop动态代理技术(底层分析)
1.导入aop的相关坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2.创建目标接口和目标类
public interface TargetInterface {
public void save();
}
public class Target implements TargetInterface{
public void save() {
System.out.println("save running....");
}
}
3.创建切面类
public class MyAspect {
public void before(){
System.out.println("前置增强...");
}
public void afterReturning(){
System.out.println("后置增强...");
}
//ProceedingJoinPoint://切入点
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕前增强...");
//切点方法,spring封装好的
Object proceed = point.proceed();
System.out.println("环绕后增强...");
return proceed;
}
}
4.将目标和切面类的对象创建权交给spring
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
</beans>
5.在applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<aop:before method="before" pointcut="execution(public void com.hao.aop.Target.save())"></aop:before>
<aop:after method="afterReturning" pointcut="execution(public void com.hao.aop.Target.save())"></aop:after>
<aop:around method="around" pointcut="execution(public void com.hao.aop.Target.save())"></aop:around>
</aop:aspect>
</aop:config>
</beans>
结果:

切点表达式的抽取,为了方便以后更改
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置目标对象-->
<bean id="target" class="com.hao.aop.Target"/>
<!-- 切面对象-->
<bean id="aspect" class="com.hao.aop.MyAspect"></bean>
<!-- 配置织入:告诉spring框架哪些方法需要进行哪些增强(前置增强、后置增强)-->
<aop:config>
<!-- 声明切面 切面=切点+通知-->
<aop:aspect ref="aspect">
<!-- 抽取切入点表达式-->
<aop:pointcut id="myPointcut" expression="execution(public void com.hao.aop.Target.save())"></aop:pointcut>
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
<aop:after method="afterReturning" pointcut-ref="myPointcut"></aop:after>
<aop:around method="around" pointcut-ref="myPointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>
spring-xml实现aop-通知的种类的更多相关文章
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- Spring基础——AOP通知
spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...
- Spring实战(十一) 在Spring XML中配置AOP
如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- Spring 中基于 AOP 的 XML架构
Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...
随机推荐
- Linux服务器 I/O 原理和流程
计算机I/OI/O在计算机中指Input/Output,IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间 ...
- np.vectorize()和crosstab()和pivotTab()函数解释
numpy.vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None) Parameter ...
- ZYNQ生成一个工程的基本步骤
Zynq 7000 SoC 是业界首款All Programmable SoC 组成: PL(FPGA部分) PS(ARM部分) PL和PS数据传输的 高效接口:AXI和ACP PS: 处理系统(Pr ...
- 4月18日 python学习总结 异常处理、网络编程
一. 异常 1.什么是异常 异常是错误发生的信号,程序一旦出错,如果程序中还没有相应的处理机制 那么该错误就会产生一个异常抛出来,程序的运行也随之终止 2.一个异常分为三部分: 1.异常的追踪信息 2 ...
- “exec: "ssh-keygen": executable file not found in %PATH%” 问题解决
set PATH=%PATH%;C:\Program Files (x86)\Git\bin bash start.sh 将以上内容保存为start.bat,放在boot2docker根目录下,管理员 ...
- 什么是内部类?Static Nested Class和Inner Class的不同?
内部类就是在一个类的内部定义的类,内部类中不能定义静态成员,内部类可以直接访问外部类中的成员变量,内部类可以定义在外部类的方法外面,也可以定义在外部类的方法体中.在方法外部定义的内部类前面可以加上st ...
- C# winform自己的窗体不抢夺其他窗体的光标
在你的form里加入: protected override CreateParams CreateParams { get { var result = base.CreateParams; ; r ...
- 如何理解 Spring 中的代理?
将 Advice 应用于目标对象后创建的对象称为代理.在客户端对象的情况下,目 标对象和代理对象是相同的. Advice + Target Object = Proxy
- Python turtle 模块可以编写游戏,是真的吗?
1. 前言 turtle (小海龟) 是 Python 内置的一个绘图模块,其实它不仅可以用来绘图,还可以制作简单的小游戏,甚至可以当成简易的 GUI 模块,编写简单的 GUI 程序. 本文使用 tu ...
- 【leetcode 29】 两数相除(中等)
题目描述 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 整数 ...