Spring AOP

1. AOP思想

AOP: Aspect Oriented Programming, 面向切面编程;
OOP: Object Oriented Programming, 面向对象编程;
AOP看起来更像是OOP的补充和完善。在OOP中,通过引入封装、继承、多态等特性来建立一种对象的层次结构,这通常可以表示对象的纵向关系,并不适合描述对象的横向关系。例如日志功能,它的代码散布在各个层次对象结构中,并且与对象的核心功能毫无关系。对于其他的代码,如安全性、异常处理等也是类似。这种散布在各个层次对象中,却又与对象的核心功能无关的代码就称为横切。
AOP技术是采用一种称为“横切”的技术,通过将对象剖解开来,将影响多个类的公共代码提取出来封装为一个可重用的模块,并将其命名为“Aspect”,即切面。对于系统中一些分布在多个模块中需要解决的共同问题,交由AOP技术解决。
切面,简单来说就是与业务核心无关的,被多个业务共同调用的逻辑或者责任封装而成,便于减少系统的重复代码,降低模块之间的耦合性,有利于未来的可维护性和扩展性。
横切将系统分为两个部分:核心关注点和横切关注点。业务的处理主要流程是核心关注点,与业务关系不大的是横切关注点。横切关注点通常位于核心关注点的各个位置中,并且核心关注点中各处的横切关注点的功能都是差不多的,例如:日志、权限认证等。

2. AOP核心概念

2.1 横切关注点:
对哪些方法或者字段进行拦截,拦截之后需要进行什么处理,这些关注点就称为横切关注点。
2.2 切面(aspect):
类是对物体特征的抽象,切面是对横切关注点的抽象。
2.3 连接点(joinpoint):
连接点就是拦截到的点,因为spring只支持方法类型的连接点,所以spring连接点实际上就是指拦截到的方法,实际上连接点还可以是字段或者构造器(spring不支持)。
2.4 切入点(pointcut):
对连接点进行拦截的定义
2.5 通知(advice):
所谓的通知就是指在拦截到连接点之后需要执行的代码,通常分为前置、后置、异常、最终、环绕五大类通知。
2.6 目标对象:
代理的目标对象。
2.7 织入(weave)
将切面应用到目标对象,并导致代理对象生成的过程。
2.8 引入(introduction):
在不改变切面代码的前提下,在运行期动态地为类添加方法或者字段。

3. Spring对AOP的支持

Spring AOP代理由spring IOC负责生成、管理,其依赖关系也是由spring IOC负责管理。因此,Spring AOP可以直接使用容器中的其他bean作为代理,这种关系由spring 依赖注入提供。
Spring 创建代理的规则为:
1. 默认使用JDK动态代理来创建代理,这样就可以为任何接口创建代理了;
2. 当需要创建代理的是类而不是接口时,Spring 会切换为Cglib代理;
Spring AOP编程主要包括3个部分:
1. 定义普通业务组件;
2. 定义切入点,一个切入点可能会横切多个业务组件;
3. 定义增强处理,增强处理就是Spring AOP为业务组件织入的处理动作;
Spring AOP编程的关键点在于定义切入点以及增强处理,一旦定义了合适的切入点以及增强处理动作,那么Spring AOP框架将会自动生成AOP代理,代理对象额方法=增强处理+被代理对象的方法;
Spring AOP的xml模板文件aop.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-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

</beans>

下面为Spring AOP的简单示例:
注意,要成功运行起Spring AOP的代码,仅仅使用Spring提供给开发者的jar是不够的,还需要从网上下载额外的两个jar包:
1. aopalliance.jar
2. aspectjweaver.jar
下面是spring aop的XML实现方式:
首先定义一个接口:

public interface HelloWorld {
    public void sayHello();
    public void doPrint();
}

定义两个接口实现类:

public class HelloWorldImp1 implements HelloWorld{
    public void sayHello(){
        System.out.println("1:hello world.");
    }
    public void doPrint(){
        System.out.println("1:do print.");
    }
}


public class HelloWorldImp2 implements HelloWorld {
    public void sayHello(){
        System.out.println("2:hello world.");
    }
    public void doPrint(){
        System.out.println("2:do print.");
    }
}

定义一个横切关注点:

public class PrintTime {
    public void printTime(){
        System.out.println("current time =  "+System.currentTimeMillis());
    }
}

aop.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-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <bean id="helloWorldImp1" class="com2.HelloWorldImp1"></bean>
    <bean id="helloWorldImp2" class="com2.HelloWorldImp2"></bean>
    <bean id="printTimeHandler" class="com2.PrintTime"></bean>
    <aop:config>
        <aop:aspect id="time" ref="printTimeHandler">
            <aop:pointcut expression="execution(* com2.HelloWorld.*(..))" id="allMethod"/>
            <aop:before method="printTime" pointcut-ref="allMethod" />
            <aop:after method="printTime" pointcut-ref="allMethod" />
        </aop:aspect>
    </aop:config>
</beans>

测试Spring AOP程序:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 {
    public static void main(String args[]){
        ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
        HelloWorld imp1=(HelloWorld)context.getBean("helloWorldImp1");
        HelloWorld imp2=(HelloWorld)context.getBean("helloWorldImp2");
        imp1.doPrint();
        imp2.doPrint();
    }
}

新增一个横切关注点,其功能是记录日志:

public class LogHandler {
    public void logBefore(){
        System.out.println("log before!");
    }
    public void logAfter(){
        System.out.println("log after!");
    }
}

修改aop.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-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <bean id="helloWorldImp1" class="com2.HelloWorldImp1"></bean>
    <bean id="helloWorldImp2" class="com2.HelloWorldImp2"></bean>
    <bean id="printTimeHandler" class="com2.PrintTime"></bean>
    <bean id="logHandler" class="com2.LogHandler" ></bean>
    <aop:config>
        <aop:aspect id="time" ref="printTimeHandler" order="2" >
            <aop:pointcut expression="execution(* com2.HelloWorld.*(..))" id="allMethod"/>
            <aop:before method="printTime" pointcut-ref="allMethod" />
            <aop:after method="printTime" pointcut-ref="allMethod" />
        </aop:aspect>
        <aop:aspect id="log" ref="logHandler" order="1" >
            <aop:pointcut expression="execution(* com2.HelloWorld.*(..))" id="allMethod1"/>
            <aop:before method="logBefore" pointcut-ref="allMethod1" />
            <aop:after method="logAfter" pointcut-ref="allMethod1" />
        </aop:aspect>
    </aop:config>
</beans>

在aop.xml配置文件中,要想logHandler的织入顺序在printHandler顺序之前,可以采用<aop:aspect>标签中的order属性;
<aop:aspect>标签有一个order属性,order属性中的数字就是横切关注织入的顺序。Spring AOP默认采用aspect定义的顺序作为横切关注点的织入顺序;

如果只想对接口中的某些方法进行织入,修改<aop:pointcut>标签中的expression属性即可:

<?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-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <bean id="helloWorldImp1" class="com2.HelloWorldImp1"></bean>
    <bean id="helloWorldImp2" class="com2.HelloWorldImp2"></bean>
    <bean id="printTimeHandler" class="com2.PrintTime"></bean>
    <bean id="logHandler" class="com2.LogHandler" ></bean>
    <aop:config>
        <aop:aspect id="time" ref="printTimeHandler" order="2" >
            <aop:pointcut expression="execution(* com2.HelloWorld.do*(..))" id="allMethod"/>
            <aop:before method="printTime" pointcut-ref="allMethod" />
            <aop:after method="printTime" pointcut-ref="allMethod" />
        </aop:aspect>
        <aop:aspect id="log" ref="logHandler" order="1" >
            <aop:pointcut expression="execution(* com2.HelloWorld.say*(..))" id="allMethod1"/>
            <aop:before method="logBefore" pointcut-ref="allMethod1" />
            <aop:after method="logAfter" pointcut-ref="allMethod1" />
        </aop:aspect>
    </aop:config>
</beans>

在上述aop.xml配置文件中,printTimeHandler只会织入HelloWorld接口中以do开头的方法;而logHandler只会织入Helloworld接口中以say为开头的方法;

参考博客:http://www.cnblogs.com/xrq730/p/4919025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值