首页 > 代码库 > spring boot 中 事务配置
spring boot 中 事务配置
package org.whm.appcore; import java.util.Properties; import javax.sql.DataSource; import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.interceptor.TransactionInterceptor; @Component public class TxConfig { @Autowired private DataSource dataSource; @Autowired private TransactionInterceptor txAdvice; // 创建事务管理器 @Bean(name = "txManager") public DataSourceTransactionManager getTx(@Autowired DataSource ds) { DataSourceTransactionManager dsTx = new DataSourceTransactionManager(ds); return dsTx; } // 创建事务通知。。 @Bean(name = "txAdvice") public TransactionInterceptor getAdvisor(@Qualifier("txManager") DataSourceTransactionManager txManager) throws Exception { TransactionInterceptor tsi = new TransactionInterceptor(); Properties properties = new Properties(); properties.setProperty("get*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception,readOnly"); tsi.setTransactionAttributes(properties); return tsi; } @Bean public BeanNameAutoProxyCreator txProxy() { BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator(); creator.setInterceptorNames("txAdvice"); creator.setBeanNames("*Service", "*ServiceImpl"); creator.setProxyTargetClass(true); return creator; } }
spring boot 中 事务配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。