• 【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;
      }
    }
    
  • 相关阅读:
    Spring总结(三)
    lucene全文检索
    知识点
    postman本地测试post接口
    第一份任务,写接口验证接口写文档
    spring Date JPA的主要编程接口
    springmvc之格式化要显示的小数或者日期。
    限制action所接受的请求方式或请求参数
    正则表达式映射
    Url通配符映射
  • 原文地址:https://www.cnblogs.com/achievec/p/6686995.html
Copyright © 2020-2023  润新知