Spring学习笔记(三)--面向切面编程AoP

Spring以控制反转IoC和切面编程AoP这两样先进的设计理念为基础,统一了应用对象的查找、配置和生命周期的管理,分离了业务与基础服务中不同

注点,使得开发人员可以基于java对象轻松的实现与EJB同样强大的功能。


AOP经常被定为为一种编程技术,用来在系统中提升业务的分离,系统有很多组件组成,每一个组件负责一部分功能。然而,这些组件也常带有一些核

功能外的其他附带功能,如日志、事务管理、安全等,经常融入到一些其他的功能模块中去。这些系统服通常叫做交叉业务,这是因为它们总是分

布在系统的很多组件当中,通过将这些业务分部在多个组件中,你的代码将引入双重复性。--摘自《Spring In Action》


实现AOP的技术,主要分为两大类:一是采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;二是采用静态

织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码。


AOP使用场景

AOP用来封装横切关注点,具体可以在下面的场景中使用:

1.日志跟踪,能够剥离核心功能业务和日志这种非核心功能业务

2.性能优化,能够剥离核心功能业务和性能记录这种非核心功能业务

3.错误处理,通过环绕功能,做业务错误的后处理

4.其他如懒加载,事务,资源池等等

AOP相关概念

通知(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 AOP

一般通过配置Spring的bean的xml文件来进行,配置方式有四种方式:

1. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象

2. 配置ProxyFactoryBean,显示地设置advisors,advice,target等 

3. 通过<aop:config>来配置

4. 通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点

也可以直接使用ProxyFactory来以编程的方式使用Spring AOP,通过ProxyFactory提供的方法可以设置target对象, advisor等相关配置,最终通过

getProxy()方法来获取代理对象

Spring AOP实例

下面我们通过一个Spring的实例来学习AOP是如何使用的,例子来自于《Spring In Action》,讲述的是一个生活里面的场景,人们观看演出,
在这个演出中,我们主要关注的肯定是演出本身,而不是灯光音效之类的。但是,一个完整的,好的演出,要是没有灯光音效,那简直就像
回到了上个世纪的黑白无声电影时代,那这个演出肯定是无味、失败的。
那么,我们如何做到只关注表演本身(主体),而把一些表演的过程中自动需要添加上去的东西,如找座位坐下,开灯,表演完成后鼓掌或是
嘘声,把这些动作放到一个程序主体之外呢?通过Spring的AOP就能完成。
首先,定义一个AOP的切面类,用来定义各种响应事件的实现:

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");
    }
}

表演者接口:Performer.java

package com.spring.test.action1;

public interface Performer {
    void perform() throws PerformanceException;
}

表演者实现类:Instrumentalist.java

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();
}

内部Bean接口实现类:Saxophone.java

package com.spring.test.setter;

public class Saxophone implements Instrument {
    public Saxophone(){}
    public void play() {
        System.out.println("TOOT TOOT TOOT");
    }
}

最关键的地方来了,如何配置bean文件,让它在执行表演的时候,把切面织入到表演的过程中来? 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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
    
    <bean id="audience" class="com.spring.test.aop.Audience"/>
    
    <bean id="sax" class="com.spring.test.setter.Saxophone"/>
    <bean id="kenny" class="com.spring.test.setter.Instrumentalist">
         <property name="song" value="Jingle Bells" />
         <property name="age" value="25" />
         <property name="instrument" ref="sax"/>
    </bean>
    
    <aop:config proxy-target-class="true">
        <aop:aspect ref="audience">
            <aop:pointcut id="performance" expression="execution(* com.spring.test.action1.Performer.perform(..))"/>
            
            <aop:before pointcut-ref="performance" method="takeSeats"/>
            <aop:before pointcut-ref="performance" method="turnOffCellPhones"/>
            <aop:after-returning pointcut-ref="performance" method="applaud"/>
            <aop:after-throwing pointcut-ref="performance" method="demandRefund"/>
        </aop:aspect>
    </aop:config>
</beans>

来,测试一下:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值