zoukankan      html  css  js  c++  java
  • 【04-09】spring boot 多数据源

    
    @EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />
    public class ProfiledemoApplication implements TransactionManagementConfigurer {
    
        @Resource(name="jpaTransactionManager")
        private PlatformTransactionManager jpaTransactionManager;
    
      // 创建事务管理器
        @Bean(name = "swiftTransactionManager")
        public PlatformTransactionManager txManager(DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        // JPA事务管理器
        @Bean(name = "jpaTransactionManager")
        public PlatformTransactionManager jpaTransactionManager(EntityManagerFactory factory) {
            return new JpaTransactionManager(factory);
        }
    
        // 实现接口 TransactionManagementConfigurer 方法,其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
        @Override
        public PlatformTransactionManager annotationDrivenTransactionManager() {
            return jpaTransactionManager;
        }
    
    }
    
    
    @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
    		DataSourceTransactionManagerAutoConfiguration.class })
    @EnableTransactionManagement
    public class Application {
    
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    
    	@Autowired DataInitializer initializer;
    
    	@PostConstruct
    	public void init() {
    
    		CustomerId customerId = initializer.initializeCustomer();
    		initializer.initializeOrder(customerId);
    	}
    }
    
    @Configuration
    @EnableJpaRepositories("com.acme.repositories")
    class AppConfig {
    
      @Bean
      public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
      }
    
      @Bean
      public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
        return new JpaTransactionManager(emf);
      }
    
      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.H2);
        jpaVendorAdapter.setGenerateDdl(true);
        return jpaVendorAdapter;
      }
    
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean lemfb = new LocalContainerEntityManagerFactoryBean();
        lemfb.setDataSource(dataSource());
        lemfb.setJpaVendorAdapter(jpaVendorAdapter());
        lemfb.setPackagesToScan("com.acme");
        return lemfb;
      }
    }
    
  • 相关阅读:
    python接口自动化之发送post(四)
    python接口自动化之发送get(三)
    python接口自动化之fiddler使用(二)
    python读取yaml配置文件
    python接口自动化测试之http协议(一)
    python接口自动化测试之根据excel中的期望结果是否存在于请求返回的响应值中来判断用例是否执行成功
    python3读取、写入、追加写入excel文件
    python UI自动化之处理多窗口
    python UI自动化之js操作
    python UI自动化之切换iframe
  • 原文地址:https://www.cnblogs.com/achievec/p/6686995.html
Copyright © 2011-2022 走看看