在 Scala 中使用 Spring @Transactional

发布于 2024-10-06 13:47:21 字数 162 浏览 3 评论 0 原文

我们有一个混合 Java 和 Scala 的项目,它使用 Spring 事务管理。我们使用 Spring 方面将文件与 @Transactional 带注释的方法编织在一起。

问题是,Scala 类没有与 Spring 事务方面交织在一起。如何配置 Spring 来处理 Scala 中的事务?

We have a mixed Java and Scala project, which uses Spring transaction management. We are using the Spring aspects to weave the files with @Transactional annotated methods.

The problem is, that the Scala classes aren't woven with the Spring transaction aspects. How can I configure Spring to regard the transaction in Scala?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

温暖的光 2024-10-13 13:47:21

Spring 需要事务边界以 Spring 管理的 bean 开始,因此这排除了 @Transactional Scala 类。

听起来简单的解决方案就是将 @Transactional Java 类实例化为 Spring bean 的服务外观。这些可以委托给您的 Scala 服务/核心代码。

Spring needs your transaction boundary to begin with Spring-managed beans, so this precludes @Transactional Scala classes.

It sounds like the simple solution is to make service facades which are @Transactional Java classes instantiated as Spring beans. These can delegate to your Scala service/core code.

埋葬我深情 2024-10-13 13:47:21

Spring 在 Scala 中的@Transactional 支持没有什么特别之处,您无需任何 Java 代码即可使用它。只需确保您具有 bean 的“纯”特征,其实现将使用 @Transactional 注释。您还应该声明一个 PlatformTransactionManager 类型的 bean(如果您使用基于 .xml 的 Spring 配置,则应该使用“transactionManager”作为 bean 名称,请参阅 EnableTransactionManagement 的 JavaDoc 了解详细信息)。另外,如果您使用基于注释的配置类,请确保将这些类放置在它们自己的专用文件中,即不要将任何其他类(伴生对象可以)放置在同一文件中。这是简单的工作示例:

SomeService.scala:

trait SomeService {
  def someMethod()
}

// it is safe to place impl in the same file, but still avoid doing it
class SomeServiceImpl extends SomeService {
  @Transactional
  def someMethod() {
    // method body will be executed in transactional context
  }
}

AppConfiguration.scala:

@Configuration
@EnableTransactionManagement
class AppConfiguration {
  @Bean
  def transactionManager(): PlatformTransactionManager = {
    // bean with PlatformTransactionManager type is required
  }

  @Bean
  def someService(): SomeService = {
    // someService bean will be proxied with transaction support
    new SomeServiceImpl
  }
}

// companion object is OK here
object AppConfiguration {
  // maybe some helper methods  
}

// but DO NOT place any other trait/class/object in this file, otherwise Spring will behave incorrectly!

There is nothing special about Spring's @Transactional support in Scala and you can use it without any Java code. Just make sure that you have "pure" traits for beans, which implementations would use @Transactional annotation. You should also declare a bean with PlatformTransactionManager type (if you are using .xml-based Spring configuration, you should use "transactionManager" for bean name, see EnableTransactionManagement's JavaDoc for details). Also, if you are using annotation-based configuration classes, be sure that these classes are placed in their own dedicated files, i.e. don't place any other classes (companion object is OK) in the same file. Here is simple working example:

SomeService.scala:

trait SomeService {
  def someMethod()
}

// it is safe to place impl in the same file, but still avoid doing it
class SomeServiceImpl extends SomeService {
  @Transactional
  def someMethod() {
    // method body will be executed in transactional context
  }
}

AppConfiguration.scala:

@Configuration
@EnableTransactionManagement
class AppConfiguration {
  @Bean
  def transactionManager(): PlatformTransactionManager = {
    // bean with PlatformTransactionManager type is required
  }

  @Bean
  def someService(): SomeService = {
    // someService bean will be proxied with transaction support
    new SomeServiceImpl
  }
}

// companion object is OK here
object AppConfiguration {
  // maybe some helper methods  
}

// but DO NOT place any other trait/class/object in this file, otherwise Spring will behave incorrectly!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文