使用 Spring 声明式事务的 Wicket
可以在 Spring 容器外部使用 Spring 框架的 @Transactional 支持。参考文档中有关于 AspectJ 方面的章节。我正在尝试在我的检票口应用程序中使用它,但没有取得积极的结果。
应用程序上下文.xml:
<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" />
<context:annotation-config />
<context:component-scan base-package="com.wicket.app"/>
<context:spring-configured />
<bean id="annotationTransactionAspect" factory-method="aspectOf"
class="org.springframework.transaction.aspectj.AnnotationTransactionAspect">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
在由 @Configurable 注释的表单类中,我有:
@Transactional
public void process(IFormSubmittingComponent submittingComponent) {
super.process(submittingComponent);
getDao().getEntityManager().flush();
}
堆栈跟踪:
org.apache.openjpa.persistence.TransactionRequiredException: Can only perform operation while a transaction is active.
It is possible to use the Spring Framework's @Transactional support outside of a Spring container. In reference documentation is chapter about AspectJ aspect. I'm trying to use it in my wicket application, but with no positive result.
application-context.xml:
<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" />
<context:annotation-config />
<context:component-scan base-package="com.wicket.app"/>
<context:spring-configured />
<bean id="annotationTransactionAspect" factory-method="aspectOf"
class="org.springframework.transaction.aspectj.AnnotationTransactionAspect">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
In my form class annotated by @Configurable, I have:
@Transactional
public void process(IFormSubmittingComponent submittingComponent) {
super.process(submittingComponent);
getDao().getEntityManager().flush();
}
Stack trace:
org.apache.openjpa.persistence.TransactionRequiredException: Can only perform operation while a transaction is active.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能可以使用 AspectJ load-time-weaving,但对于一个简单的问题来说,这是一个非常复杂的解决方案。
如果您需要声明性事务,那么我建议您将事务逻辑从 wicket 组件移至 Spring bean,并从 wicket 对象调用 Spring bean。 Spring bean 将具有事务注释,并且将由 Spring 容器正确代理。
You might be able to get this working using AspectJ load-time-weaving, but that's a very complex solution for a simple problem.
If you need declarative transactions, then I suggest you move the transactional logic from the wicket component down into a Spring bean, and invoke the Spring bean from the wicket object. The Spring bean would have the transactional annotations, and would be proxied correctly by the Spring container.
我没有使用 Wicket 的经验。但是你的“表单类”(包含用
@Transactional
注释的方法的类)是Spring托管代码吗?即谁创建了类的实例?如果不是,Spring 将不会提供
@Transactional
支持(@Autowired
也不会工作,等等)。I have no experience with Wicket. But is your 'form class' (the one that contains method annotated with
@Transactional
) Spring managed code? i.e. Who creates the instances of the class?If it's not, that Spring will not provide
@Transactional
support (neither will@Autowired
work, etc).