读书人

精通Spring事务管理的请进!该怎么解决

发布时间: 2012-02-01 16:58:19 作者: rapoo

精通Spring事务管理的请进!
小弟现在要做这样一件事情:目前项目中用到的开发框架是:SSH,目前想实现监听用户对数据的增加、删除、修改操作,作为日志保存起来。现在的思路是:使用spring的事务监听机制,在Service层对指定的方法做监听。我现在已经做了以下配置:
<bean id="operateListener" class="com.util.listener.OperationListener"></bean>
<aop:config>
<aop:aspect id="TestAspect" ref="operateListener">
<!--配置com.spring.service包下所有类或接口的所有方法-->
<aop:pointcut id="businessService"
expression="execution(* com.domain.manager.*.*(..)) and args(...) " />
<aop:before pointcut-ref="businessService" method="doBefore"/>
<aop:after pointcut-ref="businessService" method="doAfter"/>
<aop:around pointcut-ref="businessService" method="doAround"/>
<aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>
</aop:aspect>
</aop:config>
监听类是:
public class OperationListener
{
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}

public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}

public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}

public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget().getClass().getName()
+ "." + jp.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}



}
但现在一直没有实现监听效果,请高手请教!!!!!!

[解决办法]
<aop:aspect id="testAspect" ref="operateListener">
<!--配置com.spring.service包下所有类或接口的所有方法-->
<aop:pointcut id="businessService"
expression="execution(* com.domain.manager.*.*(..))" />
试试
[解决办法]
我今天搞了一天了,还是没有弄出来,很悲剧,不知道为什么,出异常了就是不回滚,等高手出现,纠结ing。。。
[解决办法]
<aop:pointcut id="businessService"
expression="execution(* com.domain.manager.*.*(..)) and args(...) " />
你这个不对吧, 改成这样试试:
expression="execution(* com.domain.manager.*.*(..))" 不知道你的包结构是怎样的,这里你在后面的
and args(...) 很是怪异。参数匹配使用前面第一个括号里面的规则。
[解决办法]
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.sailing.app.uupms..*.*(..))"


advice-ref="txAdvice"/>
</aop:config>
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) 其中带问号的modifiers-pattern?(public/protected) 和 declaring-type-pattern? throws-pattern? 可以不填
可见execution(* com.sailing.app.uupms..*.*(..))代表com.sailing.app.uupms包下以及子包下的所有类
以execution(* *..BookManager.save(..))为列子
第一颗* 代表ret-type-pattern 返回值可任意,
*..BookManager 代表任意Pacakge里的BookManager类。
如果写成com.xyz.service.* 则代表com.xyz.service下的任意类
com.xyz.service..* com.xyz.service则代表com.xyz.service及其子package下的任意类
save代表save方法,也可以写save* 代表saveBook()等方法
(..) 匹配0个参数或者多个参数的,任意类型
(x,..) 第一个参数的类型必须是X
(x,,,s,..) 匹配至少4个参数,第一个参数必须是x类型,第二个和第三个参数可以任意,第四个必须是s类型。

看看资料上说的,自己看看有没有地方写错的。
[解决办法]
<aop:pointcut id="businessService"
expression="execution(* com.domain.manager.*.*(..)) and args(...) " />
这里的 and args(...)??? 这个确实有点怪异。。没见过这样写的。。。
[解决办法]
<!-- 事务 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>

</tx:attributes>
</tx:advice>
<!-- 事务切面 -->
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.wr.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<!-- 声明切面 -->
<aop:config>
<aop:pointcut expression="execution(public * com.wr.service.*.save*(..))" id="serviceSave"/>
<aop:aspect id="saveAspect" ref="transactionSave">
<aop:after pointcut-ref="serviceSave" method="afterSave" ></aop:after>
</aop:aspect>
</aop:config>
[解决办法]
参考参考吧
反正我的是好使 事务也监听,切面也好使

读书人网 >J2EE开发

热点推荐