首页 > java > spring的AOP的配置方式

spring的AOP的配置方式

2007年6月7日 发表评论 阅读评论
1.    Proxy代理
其中类MyThrowsAdvice实现了接口中的public void afterThrows( [Method method,] [Object args,] [Object target,] Throwable throwable );方法
<bean id=”bizOneTarget” class=”com.company.biz.impl.BizProcessImpl”/>
<bean id=”throwsAdvice” class=”com.company.advice.MyThrowsAdvice”/>
<bean id=”bizOne” class=”org.springframework.aop.framework.ProxyFactoryBean”>
            <property name=”target”><ref bean=”bizOneTargte”/></property>
            <property name=”proxyInterfaces”>
                        <value>com.company.biz.IBizProcessImpl</value>
            </property>                                                               
            <property name=”interceptorNames”>
                        <list>
                                    <value>throwsAdvice</value>
                        </list>                                              
            </property>
</bean>
 
在上面的基础上还可以增加匹配方式
<bean id=" throwsAdviser"  
      class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="advice">
         <ref local=" throwsAdvice"/>
      </property>                  
      <property name="pattern">
         <value>.*</value>
      </property>
   </bean>
2.    简化配置
如果有多个BizProcess的对象需要代理,我们在Spring配置中为每一个Bean都配置一个代理,那么配置文件的维护就成了麻烦。为此,Spring提供了比较方便的方法解决这个问题,比如BeanNameAutoProxyCreator、DefaultAdviceAutoProxyCreator和metadata autoproxying。我们采用了BeanNameAutoProxyCreator,因为他比较直观和简单
配置如下:
<bean id=”beanNameAutoProxyCreator” class=”org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator”>
            <property name=’beanNames”>
                        <list>
                                    <value>*Service</value>
                        </list>                                                             
            </property>
            <property name=”interceptorNames”>         
                        <value>throwsAdvice</value>
            </property>
</bean>
另外的一种简化配置使用 DefaultAdvisorAutoProxyCreator 自动代理生成器.
3.       spring2的良好配置方式
 以下的配置方式允许对同一个类实行多次代理,推荐使用.
 第一种方式, methodSecurityInterceptor类实现了AOP联盟的规范接口,是一种环绕通知.
   <aop:config>
      <aop:pointcut id="idempotentOperation1"
        expression="execution(* test.com.wenc.spring2.acegi.T*.*(..))" />
 
      <aop:advisor pointcut-ref="idempotentOperation1"
        advice-ref="methodSecurityInterceptor" />
   </aop:config>
 
   <bean id="methodSecurityInterceptor"
      class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
                                                           
第二种方式acegiMethod类按照spring的环绕方式实现,不需要指定接口,只要在指定方法的第一个参数为ProceedingJoinPoint.
   <aop:config>
      <aop:aspect id="acegiMethodTest2" ref="acegiMethod">
 
        <aop:pointcut id="idempotentOperation2"
           expression="execution(* test.com.wenc.spring2.acegi.T*.*(..))" />
 
        <aop:before pointcut-ref="idempotentOperation2"
           method="printLog" />
 
      </aop:aspect>
 
   </aop:config>
 
   <bean id="acegiMethod"
      class="test.com.wenc.spring2.acegi.AcegiMethod">

   </bean>

分类: java 标签: 2,757 次阅读
原文链接:http://www.wenhq.com/article/view_40.html
欢迎转载,请注明出处:亲亲宝宝
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.