`

Spring AOP运用介绍

阅读更多
  AOP作为Spring这个轻量级的容器中很重要的一部分,得到越来越多的关注,Spring的Transaction就是用AOP来管理的,今天就通过简单的例子来看看Spring中的AOP的基本使用方法。
  首先确定将要Proxy的目标,在Spring中默认采用JDK中的dynamic proxy,它只能够实现接口的代理,如果想对类进行代理的话,需要采用CGLIB的proxy。显然,选择“编程到接口”是更明智的做法,下面是将要代理的接口:

  public interface FooInterface {
    public void printFoo();
    public void dummyFoo();
  }
 
  以及其一个简单的实现:
 
  public class FooImpl implements FooInterface {
    public void printFoo() {
      System.out.println("In FooImpl.printFoo");
    }
    public void dummyFoo() {
      System.out.println("In FooImpl.dummyFoo");
    }
  }
 
  接下来创建一个Advice,在Spring中支持Around,Before,After returning和Throws四种Advice,这里就以简单的Before Advice举例:
 
  public class PrintBeforeAdvice implements MethodBeforeAdvice {
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
      System.out.println("In PrintBeforeAdvice");
    }
  }
 
  有了自己的business interface和advice,剩下的就是如何去装配它们了,首先利用ProxyFactory以编程方式实现,如下:
 
  public class AopTestMain {
    public static void main(String[] args) {
      FooImpl fooImpl = new FooImpl();
      PrintBeforeAdvice myAdvice = new PrintBeforeAdvice();
     
      ProxyFactory factory = new ProxyFactory(fooImpl);
      factory.addBeforeAdvice(myAdvice);
      FooInterface myInterface = (FooInterface)factory.getProxy();

      myInterface.printFoo();
      myInterface.dummyFoo();
    }
  }
 
  现在执行程序,神奇的结果就出现了:
 
  In PrintBeforeAdvice
  In FooImpl.printFoo
  In PrintBeforeAdvice
  In FooImpl.dummyFoo
 
  虽然这样能体会到Spring中AOP的用法,但这决不是值得推荐的方法,既然使用了Spring,在ApplicationContext中装配所需要 的bean才是最佳策略,实现上面的功能只需要写个简单的applicationContext就可以了,如下:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

  <beans>
    <description>The aop application context</description>
    <bean id="fooTarget" class="FooImpl"/>
    <bean id="myAdvice" class="PrintBeforeAdvice"/>
    <bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
     <property name="proxyInterfaces">
       <value>FooInterface</value>
     </property>
     <property name="target">
       <ref local="fooTarget"/>
     </property>
     <property name="interceptorNames">
       <list>
         <value>myAdvice</value>
       </list>
     </property>
    </bean>
  </beans>

  当然,main中的代码也要进行相应的修改:
    
  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new
             ClassPathXmlApplicationContext("applicationContext.xml");
    FooInterface foo = (FooInterface)context.getBean("foo");
    foo.printFoo();
    foo.dummyFoo();
  }
 
  现在运行一下,结果将和上面的运行结果完全一样,这样是不是更优雅?当需要更改实现时,只需要修改配置文件就可以了,程序中的代码不需任何改动。
 
  但是,这时候会发现被proxy的object中的所有方法调用时都将运行advice中的before,这显然不能满足绝大多数情况下的需要,此时,只 需借用Advisor就可以了,当然要在Advisor中利用pattern设置好哪些方法需要advice,更改applicationContext 如下:
 
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

  <beans>
    <description>The springeva application context</description>
    <bean id="fooTarget" class="FooImpl"/>
    <bean id="printBeforeAdvice" class="PrintBeforeAdvice"/>
    <bean id="myAdvisor"
          class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="advice">
        <ref local="printBeforeAdvice"/>
      </property>
      <property name="pattern">
        <value>.*print.*</value>
      </property>
    </bean>
    <bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="proxyInterfaces">
        <value>FooInterface</value>
      </property>
      <property name="target">
        <ref local="fooTarget"/>
      </property>
      <property name="interceptorNames">
        <list>
          <value>myAdvisor</value>
        </list>
      </property>
    </bean>
  </beans>

  主程序不需进行任何修改,运行结果已经变样了:

  In PrintBeforeAdvice
  In FooImpl.printFoo
  In FooImpl.dummyFoo
 
  至此,应该已经理解了Spring中AOP的使用方法,当然Spring中AOP最重要的应用是Transaction Manager,举个这方面的applicationContext例子看看:
 
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd">

  <beans>
    <bean id="propertyConfigurer"   
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location">
        <value>/WEB-INF/jdbc.properties</value>
      </property>
    </bean>
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName">
        <value>${jdbc.driverClassName}</value>
      </property>
      <property name="url">
        <value>${jdbc.url}</value>
      </property>
      <property name="username">
        <value>${jdbc.username}</value>
      </property>
      <property name="password">
        <value>${jdbc.password}</value>
      </property>
    </bean>
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="dataSource">
        <ref local="dataSource"/>
      </property>
      <property name="mappingResources">
        <value>smartmenu.hbm.xml</value>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
      </property>
    </bean>

    <bean id="transactionManager"       
          class="org.springframework.orm.hibernate.HibernateTransactionManager">
      <property name="sessionFactory">
        <ref local="sessionFactory"/>
      </property>
    </bean>
    <bean id="smartmenuTarget" class="SmartMenuHibernate">
      <property name="sessionFactory">
        <ref local="sessionFactory"/>
      </property>
    </bean>
    <bean id="smartMenu"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
        <ref local="transactionManager"/>
      </property>
      <property name="target">
        <ref local="smartmenuTarget"/>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
          <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
      </property>
    </bean>
  </beans>
 
  嗯,要想彻底理解Spring的AOP,最好还是多看看源码,开源就是好啊!

分享到:
评论

相关推荐

    spring AOP的运用

    NULL 博文链接:https://pretyliang-163-com.iteye.com/blog/1671800

    Spring AOP 1.0示例

    一个运用Spring AOP 1.0 的小示例

    Spring AOP运用Spring AOP技术,要求包含前置通知、后置通知、环绕通知、返回通知、异常返回通知。

    1、编写切面类,包含权限审核方法和日志记录方法,这两个方法将来会织入到...5、编写案例,运用Spring AOP技术,要求包含前置通知、后置通知、环绕通知、返回通知、异常返回通知。请掌握这五种通知的特点,及应用场景

    spring aop简单示例.rar

    aop的原理基于java动态代理模式,本资源是spring的aop运用简单示例,用于帮助初学者理解和运用aop技术

    SpringAOP入门

    本文主要是介绍SpringAOP的相关知识,在本文里面我能介绍了SpringAOP的实现机理,在实现过程中所用到的相关技术,最后,通过一个简单的实例来对SpringAOP进行实现,进而加读者对SpringAOP的理解,以达到熟练运用的...

    spring-springMVC开发文档和AOP详解

    spring-springMVC开发文档和AOP详解,便于学习运用框架知识

    spring AOP代理机制.docx

    sping AOP面向切面的编程,程序运行过程中动态加入所需代码等,对公共的问题进行集中处理,具体的实现有动态代理与静态代理,本文通过对AOP的代理机制,前置、后置、环绕、异常的通知进行了综合总结和运用!

    Spring 3.0就这么简单源代码

    《Spring3.0就这么简单》主要介绍了Spring3.0的核心内容,不仅讲解了Spring3.0的基础知识,还深入讨论了SpringIoC容器、SpringAOP、使用SpringJDBC访问数据库、集成Hibernate、Spring的事务管理、SpringMVC、单元...

    spring中AOP

    aop的运用,aop却面的运用。只有核心代码

    Spring高级之注解驱动开发视频教程

    最终通过一个综合案例,实现灵活运用Spring框架中的各个部分。 2、适应人群 学习spring,要有一定的Java基础,同时应用过spring基于xml的配置。(或者学习过官网的Spring课程) 学习springmvc,要有一定java web...

    j2ee的aop方式记录日志

    j2ee项目经常需要记录操作者的日志,本demo在ssh框架上运用了spring的aop,加上threadlocal(用户信息,请求信息)完成了日志记录。

    SpringBoot+AOP日志

    该项目运用SpringBoot框架,MyBatis持久层,SqlSession映射,AOP技术,对项目日志进行管理

    运用Spring框架实现简单调度

    如quartz、jcrontab、JobServer等,在这里我介绍一种比较简单的调度,没有quartz那么复杂,目的在于实用就行,这种调度就是spring提供的调度功能,该调度功能基于quartz+AOP进行实现,我们开始一个例子吧。

    开源框架 Spring Gossip

    &lt;br&gt;AOP 入门 AOP 的观念与术语都不是很直觉,可以先从代理机制(Spring 实现 AOP 的一种方式)来看看实际的例子,从而了解 AOP 的观念与各种术语。 从代理机制初探 AOP 动态代理 &lt;br&gt;AOP 观念与...

    Spring3.X编程技术与应用,完整扫描版

     丁振凡编著的《Spring3.x编程技术与应用》按 循序渐进的原则对Spring3.x的主要知识及应用体系 进行了较为系统的介绍,回答了应用开发者 最为关心的一些话题,目的是帮助读者快速理解和运用相关知识。 《Spring3.x...

    Java 经典设计模式讲解以及项目实战

    设计模式简介:主要介绍各种设计模式的概念和运用场景等 设计模式综合运用:主要是笔者在实际工作中运用到的一些设计模式综合运用事例的提炼 Spring设计模式简介:主要是讲述Spring源码中运用到的一些设计模式 ...

    SpringFramework中的AOP编程之入门篇

    本系列的第二部分将更深入地介绍如何运用Spring中的所有通知类型和切入点来实现更实用的方面和面向方面设计模式。本文的目的不是要介绍构成模块化J2EE系统——即Spring框架——的所有重要元素,我们将只把注意力放在...

    Spring框架研究与探讨

    其发展历史从学术领域和研发机构 的运用开始,目前流行的Spring应用程序框架将AOP思想融入了整个框架的设计开发与应用当中。使用Spring框架固然给 我们的编程带来了好处与便利,但是同时存在着一个问题,对于...

Global site tag (gtag.js) - Google Analytics