zoukankan      html  css  js  c++  java
  • Spring in Action 入门之面向切面编程AOP

    注明:这篇文章一是当成学习笔记,二是给大家提供另一个快速理解学习Spring的参考。欢迎留言讨论,持续更新中~

    (该部分是Spring的面向切面编程AOP)

    第四章 通知Bean

    在软件编程中,散布于程序中多个地点的函数被称为“交叉事务”(日志管理、权限控制等)。从概念上来说,它们一般是与程序业务逻辑分开的,但经常却是直接嵌入其中的。把这些交叉事务与业务逻辑分离开正式面向切面编程(AOP)的作用所在,由此引入面向切面编程...

    Spring AOP的实现原理是:创建一个代理Bean,绑定通知者类(通知者类包含切面代码切点),匹配相应的方法,在目标代码中嵌入执行切面的代码。


    1. 通过配置来详细说明下Spring AOP的流程:(为了弄清楚原理,所以从配置讲起,虽然目前这种方式已经不常用了,Spring 2.0提供了更加优雅的解决方案。)

    代理Bean的配置,ProxyFactoryBean

    <bean id="duke" class="org.springframework......ProxyFactoryBean">
        <property name="target" ref="dukeTarget" />
        <property name="interceptorNames" value="audienceAdvisor" />
        <property name="proxyInterfaces" value="com.....Performer" />
    </bean>
    • target:这个属性告诉ProxyFactoryBean哪个Bean需要被代理,通俗点就是哪个Bean需要运用切面代码,target配置的就是目标代码。
    • interceptorNames:配置通知者,通知者可以按照如下方式配置:
    <bean id="audienceAdvisor" class="org.springframe....AspectJExpressionPointcutAdvisor">
        <property name="advise" ref="audienceAdvice" /> //通知者Bean,主要包含切面代码,需要实现MethodBeforeAdvice等接口,用来表示在目标代码执行前、后、抛出异常时候的切面代码
        <property name="expression" value="execution(* *.perform(..))" /> //AspectJ的切点匹配表达式,用来监测当目标代码执行perform操作时候,触发切面代码
    </bean>
    • proxyInterfaces:应该代理目标代码中的哪个接口(这个属性其实有点重复,切点表达式基本可以匹配了,自动代理就是基于这个的升级)

    2. AspectJ通过注解提供了另外一个把POJO类注解成切面的方式,比较简洁,直接在Java代码中写注解,额外的配置就是在Spring的上下文中声明一个自动代理的Bean,这样才能知道如何把@AspectJ注解的Bean转化为代理通知。

    复制代码
    import org.aspectJ.lang.annotation.Aspect;
    
    @Aspect //声明切面
    public class Audience {
    
    @Pointcut("execution(* *.perform(..))") //定义切点
    public void performance() {}
    
    @Before("performance()")  //切点之前执行
    public ....      
    
    @AfterReturning("performance()")  //切点之后执行
    public ...
    
    @AfterThrowing("performance()")  //切点抛出异常后执行
    public ...
    }
    复制代码

    3. Aspect这种切面声明的方式已经直接在代码中修改,如果要把一个普通的POJO转化成切面,那就必须获得源代码,然后改变源代码,这点是我们不希望看到的。有没有办法,让我们可以引用任何Bean,作为切面呢? Spring 2.0提供了<aop:aspect>,是一个把POJO转化为切面的优雅方案。

    复制代码
    <aop:config>
    <aop:aspect ref="audience">  //切面代码Bean
    <aop:pointcut id="performance" expression="excution(* *.performance(..))"  />   //定义切点
    
    <aop:before  method="..."  pointcut-ref="performance" />  //切面代码具体方法,在切点前执行(下同)
    <aop:after-returnning  method="..."  pointcut-ref="performance" />
    <aop:after-throwing  method="..."  pointcut-ref="performance" />
    
    </aop:aspect>
    </aop:config>
    复制代码

    总结:虽然Spring AOP对于大多数切面程序来说是足够了,但Java的构造函数与普通方法是有区别的(不能被继承),这使得Spring基于代理的AOP不能实现对象创建过程的通知。AspectJ实现的切面控制独立于Spring,提供了Spring AOP不可能实现的多种切点类型,这方面特性一般也没用到,有需要的朋友可以深入研究。

    -- 杨华辉 Switching and Intelligent Control Research Center Beijing University of Posts and Telecommunications E-mail: huahuiyang(at)gmail.com
     
    标签: Springjava
  • 相关阅读:
    字节输入流抽象类InputStream
    CentOS6.2使用yum安装LAMP及phpMyadmin(转)
    文件字节流类(不是抽象的)FileInputStream和FileOutputStream
    字节输出流抽象类OutputStream
    编译安装GD库出错解决(转)
    关于empty函数的判断
    PHP获取用户的用户的真实IP地址(转)
    字符输入输出流与字节输入输出流
    关于empty函数的输出
    thinkphp 多表 事务(转)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2752199.html
Copyright © 2011-2022 走看看