Spring注释事务中的全局配置?

时间:2014-08-21 08:23:19

标签: java spring spring-transactions spring-annotations

我们使用注释@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>

但我想在注释服务中使用这种配置。这可能吗?

1 个答案:

答案 0 :(得分:1)

我认为您不能将该xml转换为@Transactional。该注释将放在方法和/或类上,用法不同。

您需要以不同的方式思考:例如,如果您有一个包含许多get*方法的类,那么您可以在类级别放置@Transactional(readOnly=true),然后为您不希望成为的每个方法{ {1}}您放置了另一个readOnly。此外,您可以在接口上放置@Transactional(readOnly=false),如果您可以为许多类创建通用接口,则可以在一个位置定义事务行为:在接口中。