获取容器中的bean
@Component public class ApplicationContextUtil implements ApplicationContextAware { public static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ApplicationContextUtil.applicationContext = applicationContext; } }
数据源配置
@Configuration @EnableTransactionManagement public class Gxzd37DataSourceConfig { /** * 数据源37 */ @Bean(name = "dataSource37") @ConfigurationProperties(prefix = "spring.37datasource.druid") public DruidDataSource dataSource() { return new DruidDataSource(); } @Bean(name = "jdbcTemplate37") public JdbcTemplate jdbcTemplate(@Qualifier("dataSource37") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "namedParameterJdbcTemplate37") public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("dataSource37") DataSource dataSource) { return new NamedParameterJdbcTemplate(dataSource); } @Bean("transactonManager37") public PlatformTransactionManager getTransactionManager(@Qualifier("dataSource37")DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } } @Configuration @EnableTransactionManagement public class Gxzd47DataSourceConfig { /** * 数据源47 */ @Bean(name = "dataSource47") @ConfigurationProperties(prefix = "spring.gxzd47datasource.druid") public DruidDataSource dataSource() { return new DruidDataSource(); } @Bean(name = "jdbcTemplate47") public JdbcTemplate jdbcTemplate(@Qualifier("dataSource47") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "namedParameterJdbcTemplate47") public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("dataSource47") DataSource dataSource) { return new NamedParameterJdbcTemplate(dataSource); } @Bean("transactonManager47") public PlatformTransactionManager getTransactionManager(@Qualifier("dataSource47")DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
事务注解
/** * @description: 共享字典手动事务注解 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface GxzdTranstionManager { String[] transactonManagers() default {}; }
切面类
@Component @Aspect @Order(-1) public class GxzdTranstionManagerAOP{ @Pointcut(value = "@annotation(cn.crl.zdgl.config.GxzdTranstionManager)") public void methodPointcut(){} @Around(value = "methodPointcut()") public Object transactionalAspectAround(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature)point.getSignature(); Method method = signature.getMethod(); GxzdTranstionManager annotation = method.getAnnotation(GxzdTranstionManager.class); String[] managers = annotation.transactonManagers(); // 放入栈中,保证先进后出,防止ransaction synchronization is not active Stack<PlatformTransactionManager> managerStack = new Stack<>(); Stack<TransactionStatus> statusStack = new Stack<>(); if(managers.length > 0){ for (String managerName : managers) { PlatformTransactionManager manager = (PlatformTransactionManager)ApplicationContextUtil.applicationContext.getBean(managerName); TransactionStatus transactionStatus = manager.getTransaction(new DefaultTransactionDefinition()); managerStack.push(manager); statusStack.push(transactionStatus); } } try { Object proceed = point.proceed(); while (!managerStack.isEmpty()){ managerStack.pop().commit(statusStack.pop()); } return proceed; } catch (Throwable throwable) { throwable.printStackTrace(); while (!managerStack.isEmpty()){ managerStack.pop().rollback(statusStack.pop()); } throw new Exception(throwable); } } }
业务类
@Service public class Service { @Autowired @Qualifier("jdbcTemplate37") private JdbcTemplate jdbcTemplate37; @Autowired @Qualifier("jdbcTemplate47") private JdbcTemplate jdbcTemplate47; @GxzdTranstionManager(transactonManagers = {"transactonManager37","transactonManager47"}) public Integer add(Object object){ new OtherDao(jdbcTemplate37).add(object)); new OtherDao(jdbcTemplate47).add(object)); return 1; } }
标签:return,Qualifier,数据源,手动,Bean,dataSource,注解,new,public From: https://www.cnblogs.com/szc1998/p/18087550