【问题标题】:Spring boot with transactional configuration具有事务配置的 Spring Boot
【发布时间】:2015-06-02 17:51:52
【问题描述】:

我是 Spring Boot 的新手。我试图将 Spring Boot 与 hibernate 和 mysql DB 一起使用。我试图搜索如何使用 spring boot 来使用 spring 的事务配置。在具有 xml 文件的普通 Spring 应用程序中,您使用 aop 定义事务,如下所示

<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!--the transactional advice (what 'happens'; see the
<aop:advisor/>bean below)-->
<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>
<!--don't forget the DataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
     <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
     <property name="username" value="scott"/>
     <property name="password" value="tiger"/>
</bean>
<!--similarly, don't forget the PlatformTransactionManager-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
</bean>

使用上述配置,您可以要求 spring 将只读事务附加到仅 get* 方法和默认事务到所有其他方法。

你如何使用 Spring Boot 实现这一点(使用通配符在方法上定义事务 aop)?

尝试在 google 上搜索此内容,但找不到任何内容。 :(

请指导我找到解决方案或任何预先存在的链接。

谢谢

【问题讨论】:

  • 要么将@Transactional(readOnly="true") 放在它关注的那些方法上。如果您想像以前一样使用通配符,请禁用交易的自动魔法并将您的 xml 包含在 @ImportResource() 中或将其移植到 java config。
  • 谢谢@M。迪南。您的答案是完美的,它与 @ImportResource() 注释一起使用。 @Transactional(readOnly="true") 的问题是我需要把它放在每一个方法上。通配符 xml 方法的优点是它在每个方法上节省了额外的代码行。我想提到的另一件事是,如果没有@ImportResource() 注释,spring boot 也会读取我的applicationContext.xml 文件,因为它在类路径中(我认为)并且名称是标准的(applicationContext.xml)我会放它作为问题的答案。
  • 糟糕.. 我收回我的话。将applicationContext.xml 文件保存在类路径中并不是直接获取它。您必须在@ImportResource() 中提及它。实际上我忘了从类路径中删除application.properties 文件,并认为数据库配置来自applicationContext.xml。很抱歉造成混乱。

标签: java spring transactions spring-boot


【解决方案1】:

从参考文档中,你可以这样做

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

在您的情况下,您可以完全禁用配置。

链接在这里。

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

【讨论】:

  • Hey Arun,我的问题是如何将 xml 配置应用于 spring boot。正如您在回答中解释的那样,我遇到了排除特定配置的设置,但是如果我排除默认或 application.properties 配置,我正在努力解决如何提供 xml 配置。 Deinum 的答案是我一直在寻找的。另请阅读我对 deinum 关于 Spring Boot 默认行为的评论的回复。
【解决方案2】:

正如 M. Deinum 评论的那样,如果您不能跳过 xml 配置,那么您可以使用 @ImportResource 注释来使用它并提供您的 xml 文件名。 xml 应该在类路径中可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 2016-04-16
    • 2021-03-12
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    相关资源
    最近更新 更多