免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1230 | 回复: 0
打印 上一主题 下一主题

Spring AOP [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-18 23:28 |只看该作者 |倒序浏览
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就可以了,如下:

  
  

  
    The aop application context
   
   
   
     
       FooInterface
     
     
      
     
     
      
         myAdvice
      
     
   
  

  当然,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 如下:

  
  

  
    The springeva application context
   
   
   
      
        
      
      
        .*print.*
      
   
   
      
        FooInterface
      
      
        
      
      
        
          myAdvisor
        
      
   
  

  主程序不需进行任何修改,运行结果已经变样了:
  In PrintBeforeAdvice
  In FooImpl.printFoo
  In FooImpl.dummyFoo


  至此,应该已经理解了Spring中AOP的使用方法,当然Spring中AOP最重要的应用是Transaction Manager,举个这方面的applicationContext例子看看:

  
  

  
   
      
        /WEB-INF/jdbc.properties
      
   
   
      
        ${jdbc.driverClassName}
      
      
        ${jdbc.url}
      
      
        ${jdbc.username}
      
      
        ${jdbc.password}
      
   
   
      
        
      
      
        smartmenu.hbm.xml
      
      
        
          ${hibernate.dialect}
        
      
   

   
      
        
      
   
   
      
        
      
   
   
      
        
      
      
        
      
      
        
          PROPAGATION_REQUIRED,readOnly
          PROPAGATION_REQUIRED,readOnly
        
      
   
  


  要实现自动代理exmaple如下:
for a single Hibernate SessionFactory (alternative to JTA) -->"transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">    "sessionFactory">    "sessionFactory"/>
"transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">    "transactionManager" ref="transactionManager"/>?    "transactionAttributes">            "*">PROPAGATION_REQUIRED        "find*">PROPAGATION_REQUIRED,readOnly        
"org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    "beanNames">        *Service,*Manager        "interceptorNames">                    transactionInterceptor                        
"org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">    "transactionInterceptor" ref="transactionInterceptor"/>?
"userManager" class="some.package.UserManagerImpl" autoWire="byName"/>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/37861/showart_324019.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP