Spring以控制反转IoC和切面编程AoP这两样先进的设计理念为基础,统一了应用对象的查找、配置和生命周期的管理,分离了业务与基础服务中不同
关注点,使得开发人员可以基于java对象轻松的实现与EJB同样强大的功能。
AOP经常被定为为一种编程技术,用来在系统中提升业务的分离,系统有很多组件组成,每一个组件负责一部分功能。然而,这些组件也常带有一些核
心功能外的其他附带功能,如日志、事务管理、安全等,经常融入到一些其他的功能模块中去。这些系统服务通常叫做交叉业务,这是因为它们总是分
布在系统的很多组件当中,通过将这些业务分部在多个组件中,你的代码将引入双重复杂性。--摘自《Spring In Action》
实现AOP的技术,主要分为两大类:一是采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;二是采用静态
织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码。
AOP用来封装横切关注点,具体可以在下面的场景中使用:
1.日志跟踪,能够剥离核心功能业务和日志这种非核心功能业务
2.性能优化,能够剥离核心功能业务和性能记录这种非核心功能业务
3.错误处理,通过环绕功能,做业务错误的后处理
4.其他如懒加载,事务,资源池等等
通知(Advice): 在AOP术语中,切面的工作被称为通知。各种类型的通知包括“around”、“before”和“throws”通知。通知定义了切面是什么以及何时使用。Spring中定义了5种通知类型:Before,After,After-returning,After-throwing和Around
连接点(Joinpoint): 连接点是在应用执行过程中能够被插入切面的一个点,这个点可以是方法调用时,抛出异常时,甚至是修改一个字段时。
切点(Pointcut): 如果通知定义了切面的“什么”和“何时”,那么切点就定义了“何处”。切点的定义会匹配通知所要织入的一个或多个连接点。我们通常使用明确的类和方法名称来指定这些切点,或是利用正则表达式匹配的类和方法名称。AOP框架必须允许开发者指定切入点:例如,使用正则表达式。Spring定义了Pointcut接口,用来组合MethodMatcher和ClassFilter,可以通过名字很清楚的理解,MethodMatcher是用来检查目标类的方法是否可以被应用此通知,而ClassFilter是用来检查Pointcut是否应该应用到目标类上
切面(Aspect):切面是通知和切点的结合。通知和切点共同定义了关于切面的全部内容--它是什么,在何时和何处完成其功能
引入(Introduction): 引入允许我们向现有的类添加新的方法或属性。 Spring允许引入新的接口到任何被通知的对象。例如,你可以使用一个引入使任
何对象实现某个接口,来简化缓存。Spring中要使用Introduction, 可有通过DelegatingIntroductionInterceptor来实现通知,通过
DefaultIntroductionAdvisor来配置Advice和代理类要实现的接口
目标对象(Target Object): 包含连接点的对象。也被称作被通知或被代理对象。
AOP代理(AOP Proxy): AOP框架创建的对象,包含通知。 在Spring中,AOP代理可以是JDK动态代理
织入(Weaving): 织入是将切面应用到目标对象来创建新的代理对象的过程。切面是指定的连接点被织入的目标对象中。在目标对象的生命周期里有
多个点可以进行织入
一般通过配置Spring的bean的xml文件来进行,配置方式有四种方式:
1. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象
2. 配置ProxyFactoryBean,显示地设置advisors,advice,target等
3. 通过
4. 通过
也可以直接使用ProxyFactory来以编程的方式使用Spring AOP,通过ProxyFactory提供的方法可以设置target对象, advisor等相关配置,最终通过
getProxy()方法来获取代理对象
package com.spring.test.aop;
public class Audience {
// 在表演之前先找到座位
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
// 在表演之前需要关掉手机
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones");
}
// 表演成功,鼓掌
public void applaud(){
System.out.println("CLAP CLAP CLAP");
}
// 表演失败,嘘声并要求退款
public void demandRefund(){
System.out.println("Boo! We want money back");
}
}
package com.spring.test.action1;
public interface Performer {
void perform() throws PerformanceException;
}
package com.spring.test.setter;
import com.spring.test.action1.PerformanceException;
import com.spring.test.action1.Performer;
public class Instrumentalist implements Performer{
private String song;
private int age;
private Instrument instrument;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public Instrumentalist(){}
public Instrumentalist(String song,int age,Instrument instrument){
this.song = song;
this.age = age;
this.instrument = instrument;
}
public void perform() throws PerformanceException {
System.out.println("Instrumentalist age:"+age);
System.out.print("Playing "+song+":");
instrument.play();
}
}
内部Bean接口:Instrument.java
package com.spring.test.setter;
public interface Instrument {
public void play();
}
package com.spring.test.setter;
public class Saxophone implements Instrument {
public Saxophone(){}
public void play() {
System.out.println("TOOT TOOT TOOT");
}
}
package com.spring.test.setter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.action1.PerformanceException;
public class test {
public static void main(String[] args) throws PerformanceException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
performer.perform();
}
}
运行结果:
The audience is taking their seats.
The audience is turning off their cellphones
Instrumentalist age:25
Playing Jingle Bells:TOOT TOOT TOOT
CLAP CLAP CLAP