【发布时间】:2014-08-22 18:10:58
【问题描述】:
我们使用带有注释性@Transactional 属性而不是XML 替代方案的Spring 事务。是否可以将某些服务方法指定为 XML 版本中声明的“只读”?
据我所知,在 XML 版本中,您可以指定方法和只读配置为:
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
但我想在注释服务中使用此配置。这可能吗?
【问题讨论】:
-
我认为您无法将该 xml 转换为
@Transactional。该注释将放置在方法和/或类上,并且用法不同。您需要换一种思路:例如,如果您有一个包含许多get*方法的类,那么您在类级别放置一个@Transactional(readOnly=true),然后对于您不想被读取的每个方法,您只需放置另一个@Transactional(readOnly=false)。此外,您可以将@Transactional放在接口上,如果您可以为许多类创建通用接口,您可以在一个地方定义事务行为:在接口中。
标签: java spring spring-transactions spring-annotations