Spring 基于XML配置的AOP入门案例
来源:互联网 发布:实时电脑远程软件 编辑:程序博客网 时间:2024/04/19 23:06
AOP是Aspect Oriented Programing的简称--面向切面编程。在软件开发中,分布于应用中多处的功能被称为横切关注点。通常这些横切关注点从概念上是与应用的业务逻辑相分离(但是往往直接嵌入到应用的业务逻辑之中)。将这些横切关注点与业务逻辑相分离正是面向切面编程(AOP)所要解决的。
AOP术语
通知(Advice) 包含5种类型:Before(在方法被调用之前调用通知)、After(在方法完成之后调用通知,无论方法执行是否成功)、After-returing(在方法成功执行之后调用通知)、After(在方法抛出异常后调用通知)和Around(通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为)
连接点(Joinpoint)连接点是程序执行的某个特定位置。一个类或一段程序代码拥有一些具有边界性质的特定点,这些代码中的特定点就称为“连接点”。连接点由两个信息确定:其一是用方法表示的程序执行点;其二是用相对点表示的方位。例如,在Test.foo()方法执行前的连接点,执行点为Test.foo(),方位为该方法执行前的位置。Spring使用切点对执行点进行定位,而方位则在增强类型中定义。
切点(Pointcut)一个切面并不需要通知应用的所有连接点,切点有助于缩小切面所通知连接点的范围。
切面(Aspect)切面是通知和切点的结合。通知和切点共同定义了关于切面的全部内容--它是什么,在何时和何处完成其功能。
引入(Introduction)允许我们向现有的类添加新方法或属性。
织入(Weaving)将切面应用到目标对象来创建新的代理对象的过程,其中在目标对象的生命周期有三处可以织入:编译器、类加载期和运行期。
下面通过一个简单的例子具体说明:
新建一个Java Project,目录结构如下:
Audience源码:
package com;import org.aspectj.lang.ProceedingJoinPoint;public class Audience {public void takeSeats(){System.out.println("-----BeforeAdvice-----\nThe audience is taking their seats.");}public void wathPerformance(ProceedingJoinPoint joinPoint){System.out.println("\n-----AroundAdvice-----");long start=System.currentTimeMillis();System.out.println("start:"+start);try {joinPoint.proceed();long end=System.currentTimeMillis();System.out.println("end:"+end);System.out.println("The performance took "+(end-start)+" milliseconds.\n");} catch (Throwable e) {System.out.println("Boo!We want our money back!\n");}}public void leaveSeats(){System.out.println("-----AfterReturningAdvice-----\nThe audience is leaving their seats.");}public void end(){System.out.println("\n-----AfterAdvice-----\nOver!");}public void demandRefund(){System.out.println("\n-----AfterThrowing-----\nBoo!We want our money back!");}}
NaughtWaiter源码:
package com;public class NaughtWaiter{public void greetTo(String name) {System.out.println("NaughtWaiter:greet to "+name+"..." );}}
Performer源码:
package com;public class Performer{public void perform() {System.out.println("\nThe performer is performing!\n" );}public void accident(){throw new RuntimeException("运行时异常!");}}
beans.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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="audience" class="com.Audience" /> <bean id="performer" class="com.Performer"/> <aop:config> <aop:aspect ref="audience"> <aop:pointcut expression="execution(* com.Performer.perform(..))" id="pointcut"/> <aop:before method="takeSeats" pointcut-ref="pointcut"/> <aop:after method="end" pointcut-ref="pointcut"/> <aop:after-returning method="leaveSeats" pointcut-ref="pointcut"/> <aop:around method="wathPerformance" pointcut-ref="pointcut"/> <aop:after-throwing method="demandRefund" pointcut="execution(* com.Performer.accident(..))"/> </aop:aspect> </aop:config> </beans>
Test源码:
package com;import java.io.File;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.FileSystemXmlApplicationContext;public class Test {public static void main(String[] args) {String path=new File("").getAbsolutePath()+"/src/com/beans.xml";BeanFactory factory=new FileSystemXmlApplicationContext(path);Performer performer=(Performer) factory.getBean("performer");performer.perform();performer.accident();}}
运行Test,结果为:
-----BeforeAdvice-----
The audience is taking their seats.
-----AroundAdvice-----
start:1430099696553
The performer is performing!
end:1430099696569
The performance took 16 milliseconds.
-----AfterReturningAdvice-----
The audience is leaving their seats.
-----AfterAdvice-----
Over!
-----AfterThrowing-----
Boo!We want our money back!
Exception in thread "main" java.lang.RuntimeException: 运行时异常!
....
1、通过FileSystemXmlApplicationContext(path)实现beans.xml的BeanDefinition信息的定位、载入和注册,并完成容器的初始化;
2、通过BeanFacorty获取Bean对象;
3、调用方法,并执行符合切面表达式的通知。
- Spring 基于XML配置的AOP入门案例
- Spring 基于XML配置的IOC入门案例
- Spring-XML配置AOP-案例
- 基于XML的spring AOP配置
- Spring的AOP配置(基于xml)
- Spring -- 基于XML的AOP通知配置
- 基于XML的Spring AOP配置
- Mybatis基于xml配置的入门案例
- Spring基于XML配置AOP
- spring aop xml方式的配置案例 以及 annotation方式的配置aop
- Spring的AOP基于xml常用的几种配置
- Spring中AOP基于XML的两种配置方法
- 基于XML配置的纯POJO Spring 2.x Aop
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP基于XML配置的管理方式
- Spring AOP(3)基于XML配置实现的示例
- spring学习总结(十):基于 XML 的配置AOP
- Spring基于xml文件的方式配置AOP
- python多线程编程(3): 使用互斥锁同步线程
- cocos2d-x3.5获取cocostuio生成的lua界面
- prim 与dijkstra的异同 POJ 2485 Highways
- session和cookie的区别是什么
- Stencil Buffer(模板缓冲区)
- Spring 基于XML配置的AOP入门案例
- 第7周项目1图形用户界面初体验(vs2008)
- 中国病人!
- Android activity 之间的相互切换以及传递值
- leetcode || 112、Path Sum
- ios中头文件交叉导入问题
- java输出99乘法表
- 关于“50% CPU 占有率”题目的内容扩展
- 点击对话框的任何位置都能拖动,模拟单机了鼠标左键