电竞比分网-中国电竞赛事及体育赛事平台

分享

SpringFramework中的AOP簡(jiǎn)單使用

 Kaka 2006-07-17
UML軟件工程組織

SpringFramework中的AOP簡(jiǎn)單使用
 AOP作為Spring這個(gè)輕量級(jí)的容器中很重要的一部分,得到越來(lái)越多的關(guān)注,Spring的Transaction就是用AOP來(lái)管理的,今天就通過(guò)簡(jiǎn)單的例子來(lái)看看Spring中的AOP的基本使用方法。

  首先確定將要Proxy的目標(biāo),在Spring中默認(rèn)采用JDK中的dynamic proxy,它只能夠?qū)崿F(xiàn)接口的代理,如果想對(duì)類(lèi)進(jìn)行代理的話(huà),需要采用CGLIB的proxy。顯然,選擇“編程到接口”是更明智的做法,下面是將要代理的接口:

  public interface FooInterface {
    public void printFoo();
    public void dummyFoo();
  }

 
  以及其一個(gè)簡(jiǎn)單的實(shí)現(xiàn):
 
  public class FooImpl implements FooInterface {
    public void printFoo() {
      System.out.println("In FooImpl.printFoo");
    }
    public void dummyFoo() {
      System.out.println("In FooImpl.dummyFoo");
    }
  }

 
  接下來(lái)創(chuàng)建一個(gè)Advice,在Spring中支持Around,Before,After returning和Throws四種Advice,這里就以簡(jiǎn)單的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以編程方式實(shí)現(xiàn),如下:
 
  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();
    }
  }
 
  現(xiàn)在執(zhí)行程序,神奇的結(jié)果就出現(xiàn)了:
 
  In PrintBeforeAdvice
  In FooImpl.printFoo
  In PrintBeforeAdvice
  In FooImpl.dummyFoo

 
  雖然這樣能體會(huì)到Spring中AOP的用法,但這決不是值得推薦的方法,既然使用了Spring,在A(yíng)pplicationContext中裝配所需要 的bean才是最佳策略,實(shí)現(xiàn)上面的功能只需要寫(xiě)個(gè)簡(jiǎn)單的applicationContext就可以了,如下:
 
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "
http://www./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>

  當(dāng)然,main中的代碼也要進(jìn)行相應(yīng)的修改:
    
  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new 
             ClassPathXmlApplicationContext("applicationContext.xml");
    FooInterface foo = (FooInterface)context.getBean("foo");
    foo.printFoo();
    foo.dummyFoo();
  }

 
  現(xiàn)在運(yùn)行一下,結(jié)果將和上面的運(yùn)行結(jié)果完全一樣,這樣是不是更優(yōu)雅?當(dāng)需要更改實(shí)現(xiàn)時(shí),只需要修改配置文件就可以了,程序中的代碼不需任何改動(dòng)。
 
  但是,這時(shí)候會(huì)發(fā)現(xiàn)被proxy的object中的所有方法調(diào)用時(shí)都將運(yùn)行advice中的before,這顯然不能滿(mǎn)足絕大多數(shù)情況下的需要,此時(shí),只 需借用Advisor就可以了,當(dāng)然要在A(yíng)dvisor中利用pattern設(shè)置好哪些方法需要advice,更改applicationContext 如下:
 
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "
http://www./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>

  主程序不需進(jìn)行任何修改,運(yùn)行結(jié)果已經(jīng)變樣了:

  In PrintBeforeAdvice
  In FooImpl.printFoo
  In FooImpl.dummyFoo

 
  至此,應(yīng)該已經(jīng)理解了Spring中AOP的使用方法,當(dāng)然Spring中AOP最重要的應(yīng)用是Transaction Manager,舉個(gè)這方面的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,最好還是多看看源碼,開(kāi)源就是好?。?/p>

 

版權(quán)所有:UML軟件工程組織

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多