首页 文章

具有事务配置的Spring启动

提问于
浏览
1

我是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)?

尝试在谷歌搜索这个但找不到任何东西 . :(

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

谢谢

2 回答

  • 0

    从参考文档中,您可以执行此操作

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

    在这种情况下,您可以完全禁用配置 .

    链接在这里 .

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

  • 0

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

相关问题