public class MethodInterceptor implements  MethodInterceptor{

        public Object invoke(MethodInvocation invocation) throws Throwable {        String targetName = invocation.getThis().getClass().getName();  
        String methodName = invocation.getMethod().getName();    
        Object[] arguments = invocation.getArguments();    
        System.out.println(targetName+" "+methodName+" "+arguments[0].toString());
        invocation.proceed();
        System.out.println("after");
        return null;
}

}
applicationContext.xml 关键代码        .....
         <!-- 事务配置 start-->
         <aop:config >
<aop:advisor
pointcut="execution(*  com.test.BO.*BO.*(..))"
advice-ref="txAdvice" />

 </aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
    <!-- 事务配置 end -->    <bean id="methodCacheAfterAdvice" class="com.test.interceptor.MethodInterceptor">     
    </bean>        <bean id="methodPointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    
      <property name="advice">    
        <ref local="methodCacheAfterAdvice"/>    
      </property>    
      <property name="patterns">    
        <list>    
            <value>.*find.*</value>   
        </list>    
      </property>    
    </bean> 
    
<bean name="passPortBOImpl_1"  
        class="com.test.BO.PassPortBOImpl">   
       <property name="passPortDAO">
<ref bean="passPortDao" />
</property>  
    </bean> 
 <bean name="passPortBOImpl" class="org.springframework.aop.framework.ProxyFactoryBean" >    
      <property name="target">    
          <ref bean="passPortBOImpl_1"/>    
      </property>    
      <property name="interceptorNames">    
        <list>    
          <value>methodPointCut</value>    
      </list>    
      </property> 
    </bean>   
  ..........
这样配好后测试public static void main(String[] args){
PassPortBO pb=(PassPortBO) ApplicationContext.getInstance().getBean("passPortBOImpl");
Account account=pb.findOneAccountByPid(1);
}
结果打印出:
$Proxy4 findOneAccountByPid 1
$Proxy4 findOneAccountByPid 1
com.test.BO.PassPortBOImpl findOneAccountByPid 1
after
after
after然后我把applicationContext.xml 里事务配置那几行删掉。结果为
com.test.BO.PassPortBOImpl findOneAccountByPid 1
after这什么原因?? PassPortBOImpl的find方法可是空的。难道配置事务后 spring代理类也被增强??

解决方案 »

  1.   

    spring代理类也被增强?
    没明白你说的意思
    但是
    在你的切入点定义中
     <aop:advisor
                pointcut="execution(*  com.test.BO.*BO.*(..))"
                advice-ref="txAdvice" />
                
         </aop:config>
    明显存在观察PassPortBOImpl,和find
      

  2.   

    嗯,的确奇怪,按道理事务代理里面是不应该有methodCacheAfterAdvice这个增强的。
    另外有事务的话还会多执行2次?