gpt4 book ai didi

java - 具有事务配置的 Spring Boot

转载 作者:太空宇宙 更新时间:2023-11-04 14:00:39 24 4
gpt4 key购买 nike

我是 Spring Boot 新手。我试图将 Spring Boot 与 hibernate 和 mysql DB 一起使用。我试图寻找如何使用 spring boot 使用 spring 的事务配置。在普通的 spring 应用程序中,您有 xml 文件,您可以使用 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)?

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

请指导我找到解决方案或任何现有的链接。

谢谢

最佳答案

从引用文档中,您可以执行此操作

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

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

链接在此。

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

关于java - 具有事务配置的 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29333942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com