使用 Spring 声明式事务的 Wicket

发布于 2024-08-17 19:33:15 字数 1050 浏览 3 评论 0原文

可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-08-24 19:33:15

可能可以使用 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.

凉风有信 2024-08-24 19:33:15

我没有使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文