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代理类也被增强??
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代理类也被增强??
解决方案 »
- axis2.wsdl文件的问题。
- jsp 导出Excel 没有表格 !在线等.....
- 连接池问题
- 如何让ServerSocket线程和Jsp页面交互
- json对象转换问题
- 大家进来讨论下J2EE程序的开发流程吧
- 新手操作數據庫問題
- 出现指定资源无法下载!!!
- 老板要求做一个电子商务软件,我对这方面不了解,希望大家能帮帮我
- 我按cn-java上实战EJB做的第一个EJB例子(最简单的),最后运行http://localhost:6888/hello/servlet/HelloServlet,结果提示“Http:404
- getWebApplicationContext().getBean("xxx");
- 关于 InterruptedException 异常
没明白你说的意思
但是
在你的切入点定义中
<aop:advisor
pointcut="execution(* com.test.BO.*BO.*(..))"
advice-ref="txAdvice" />
</aop:config>
明显存在观察PassPortBOImpl,和find
另外有事务的话还会多执行2次?