Spring 3.0 TransactionProxyFactoryBean preInterceptors AfterReturningAdvice 不起作用
我需要记录业务活动,这些活动也可以映射到生成的审计跟踪数据。我使用 Hibernate envers 作为审计跟踪机制。
我实现活动日志的方式是
- 使用具体类代理的服务类 (使用CGLIB)并扩展 TransactionProxyFactoryBean 。这就是 提供交易方面。
- 我的方法要么具有携带活动数据的基础对象 服务的返回类型或参数。
- 假设是当我在 TransactionProxyFactoryBean ;其 AfterReturningAdvice 方法 应该在交易完成后调用。
根据我的理解,基于拦截器添加到堆栈上的假设,TransactionProxyFactoryBean 的前置和后置拦截器的行为应如下。
- 建议方法运行之前的预拦截器
- Spring 启动事务
- 建议方法运行之前的后拦截器
- 主要服务方法运行
- 返回建议方法之后的后拦截器
- Spring 提交事务 返回
- 建议方法之后的预拦截器运行
但是当我对应用程序进行调试后,我发现预拦截器的返回建议方法在提交事务之前运行。
谁能指导我我做错了什么?
TransactionProxyFactoryBean配置
<bean id="fqngTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="fqngTxProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="fqngTransactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="process*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="activityLogInterceptor"
class="com.fuelquest.mothra.activitylogs.interceptors.ActivityLogInterceptor">
<property name="activityLogPostingService">
<ref bean="activityLogPostingService" />
</property>
<property name="methodList">
<list>
<value>save*</value>
<value>execute*</value>
<value>calculate*</value>
</list>
</property>
</bean>
活动拦截器Java文件定义
public class ActivityLogInterceptor implements AfterReturningAdvice {
private static final Logger logger = Logger
.getLogger(ActivityLogInterceptor.class);
private ActivityLogPostingService activityLogPostingService;
private List<String> methodList;
@SuppressWarnings("rawtypes")
@Override
public void afterReturning(Object returnValue, Method method,
Object[] methodParams, Object target) throws Throwable {
// If return type is ActivityLoggingBaseVO
if (isLoggedMethod(method.getName())) {
.......................
服务配置
<bean id="inventoryControlRuleService" parent="fqngTxProxyTemplate">
<property name="target">
<bean
class="com.fuelquest.mothra.inventorycontrol.service.impl.InventoryControlRuleServiceImpl">
<property name="assetService">
<ref bean="assetService" />
</property>
<property name="pointOfSaleService">
<ref bean="pointOfSaleService" />
</property>
<property name="inventoryService">
<ref bean="inventoryService" />
</property>
<property name="deliveryService">
<ref bean="deliveryService" />
</property>
<property name="languageCdDao">
<ref bean="languageCdDao" />
</property>
<property name="inventoryBizRulesDao">
<ref bean="inventoryBizRulesDao" />
</property>
<property name="bizRulesResultsDao">
<ref bean="bizRulesResultsDao" />
</property>
<property name="ruleEngineService">
<ref bean="ruleEngineService" />
</property>
<property name="icRuleCalculationDataDao">
<ref bean="icRuleCalculationDataDao" />
</property>
<property name="inventoryControlService">
<ref bean="inventoryControlService" />
</property>
<property name="fqngESBMessagePoster">
<ref bean="fqngESBMessagePoster" />
</property>
<property name="droolsRuleTemplateService">
<ref bean="droolsRuleTemplateService" />
</property>
<property name="uomsDao">
<ref bean="uomDao" />
</property>
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="calculate*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="execute*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="f*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
<property name="preInterceptors">
<list>
<ref bean="activityLogInterceptor"/>
</list>
</property>
</bean>
I have a requirement to log business activities that can also map to the audit trail data generated. I use Hibernate envers as the audit trail mechanism.
The way I have implemented the activities log is
- I have service classes that are proxied using concrete classes
(using CGLIB) and extend TransactionProxyFactoryBean . This is what
provides the transaction aspect. - My method either has the base object carrying the activity data as
a return type or argument of the service. - The assumption is that when I apply a pre-interceptor on the
TransactionProxyFactoryBean ; its AfterReturningAdvice method
should be called after the transaction is completed.
As per my understanding the pre and post interceptors for the TransactionProxyFactoryBean should behave as follows based on the assumptions that the interceptors are added on the stack.
- The pre-interceptors before advice method run
- Spring starts the transaction
- The post-interceptors before advice method runs
- The main service method runs
- The post-interceptors after returning advice method runs
- Spring commits the transaction
- The pre-interceptors after returning advice method runs
However when I de-bugged the application I found that the pre-interceptor's after returning advice method runs before the transaction is commited.
Can anyone please guide me as to what am I doing wrong?
TransactionProxyFactoryBean configuration
<bean id="fqngTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="fqngTxProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="fqngTransactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="process*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="activityLogInterceptor"
class="com.fuelquest.mothra.activitylogs.interceptors.ActivityLogInterceptor">
<property name="activityLogPostingService">
<ref bean="activityLogPostingService" />
</property>
<property name="methodList">
<list>
<value>save*</value>
<value>execute*</value>
<value>calculate*</value>
</list>
</property>
</bean>
Activity Interceptor Java file Definition
public class ActivityLogInterceptor implements AfterReturningAdvice {
private static final Logger logger = Logger
.getLogger(ActivityLogInterceptor.class);
private ActivityLogPostingService activityLogPostingService;
private List<String> methodList;
@SuppressWarnings("rawtypes")
@Override
public void afterReturning(Object returnValue, Method method,
Object[] methodParams, Object target) throws Throwable {
// If return type is ActivityLoggingBaseVO
if (isLoggedMethod(method.getName())) {
.......................
Service Configuration
<bean id="inventoryControlRuleService" parent="fqngTxProxyTemplate">
<property name="target">
<bean
class="com.fuelquest.mothra.inventorycontrol.service.impl.InventoryControlRuleServiceImpl">
<property name="assetService">
<ref bean="assetService" />
</property>
<property name="pointOfSaleService">
<ref bean="pointOfSaleService" />
</property>
<property name="inventoryService">
<ref bean="inventoryService" />
</property>
<property name="deliveryService">
<ref bean="deliveryService" />
</property>
<property name="languageCdDao">
<ref bean="languageCdDao" />
</property>
<property name="inventoryBizRulesDao">
<ref bean="inventoryBizRulesDao" />
</property>
<property name="bizRulesResultsDao">
<ref bean="bizRulesResultsDao" />
</property>
<property name="ruleEngineService">
<ref bean="ruleEngineService" />
</property>
<property name="icRuleCalculationDataDao">
<ref bean="icRuleCalculationDataDao" />
</property>
<property name="inventoryControlService">
<ref bean="inventoryControlService" />
</property>
<property name="fqngESBMessagePoster">
<ref bean="fqngESBMessagePoster" />
</property>
<property name="droolsRuleTemplateService">
<ref bean="droolsRuleTemplateService" />
</property>
<property name="uomsDao">
<ref bean="uomDao" />
</property>
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="calculate*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="execute*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="f*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
<property name="preInterceptors">
<list>
<ref bean="activityLogInterceptor"/>
</list>
</property>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们使用 Spring OpenSessionInViewFilter 在从 GWT 到达应用程序/Web 服务器的 HTTP 线程请求中共享相同的 Hibernate 会话。
我们还要求 Hibernate 会话可用于使用 Quartz 调度程序启动的 cron 作业。这些线程无法使用通过 OpenSessionInViewFilter 提供的 Hibernate 会话以及我们用来代理事务失败的 TransactionProxyFactoryBean 代理。因此,我们需要使用额外的 org.springframework.aop.framework.autoproxy.BeanNa meAutoProxyCreator 来代理轮询和 SV 规则服务,以便可以从 Quartz 调度程序中调用它们。
因为我们现在对同一个 bean 有 2 个事务代理; Spring 功能(例如预拦截器等)无法按预期工作,因为拦截器应用于使用 BeanNameAutoProxyCreator 创建的代理,而不是 TransactionProxyFactoryBean。
解决方案是使用 AOP 和 TX 命名空间转移到 Spring 2.x 事务,从而创建一个由 OpenSessionInViewFilter 和 Quartz 调度程序使用的代理。
We use the Spring OpenSessionInViewFilter to share the same Hibernate session across the HTTP thread request that comes to the application/web server from GWT.
We also require to have Hibernate sessions available for the cron jobs that are launched using Quartz scheduler. These threads can't use the Hibernate session made available through the OpenSessionInViewFilter and the TransactionProxyFactoryBean proxy that we use to proxy the transactions fails. Hence we needed to use an additional org.springframework.aop.framework.autoproxy.BeanNa meAutoProxyCreator to proxy the polling and SV rule service so that they can be called from the Quartz scheduler.
Because we now had 2 transactional proxies for the same bean; Spring functionality like having pre-interceptor etc was not working as expected because the interceptor was applied on the proxy created with BeanNameAutoProxyCreator and NOT TransactionProxyFactoryBean.
The solution was to move to Spring 2.x transactions using AOP and TX namespace that resulted in creating a single proxy that was utilized by both the OpenSessionInViewFilter and the Quartz scheduler.
你的理解和我的一致。
切换到 ActivityLogInterceptor 的周围建议(MethodInterceptor)会有帮助吗?如果这解决了问题,您可能需要报告错误。
Your understanding matches mine.
Would switching to an Around advice (MethodInterceptor) for ActivityLogInterceptor help? If that resolves the issue you may have a bug to report.