我正在将Spring应用程序从基于xml的旧式配置转移到基于注释的配置。
目前,我在xml中定义了所有服务,并为每个服务定义了org.springframework.transaction.interceptor.TransactionProxyFactoryBean
。
<bean id="dataServiceTarget" class="com.company.DataService">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="dataServiceTarget"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
我即将使用@Transactional
注释注释服务。
来自http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the FooService interface -->
<aop:config>
<aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
但每次我使用以readonly=true
开头的方法时,我必须在@Transactional
注释中添加get
属性:
@Transactional(readOnly = true)
public void getFoo() {
return ...;
}
我有许多方法不以get
开头,它们不应该是只读的。所以我不能在课堂上放置@Transactional(readOnly = false)
。
可能的解决方案是将类划分为只读和读写部分,但现在我们的代码真的很难。另一个解决方案是使用上面的例子。
Spring能否同时将所有get*
注释为只读方法?
任何帮助表示赞赏。