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!