我想在spring boot app中使用两个数据源,比如某些表我想使用h2,而某些使用mysql使用jpa hibernate。 在我的application.java中我正在做的是..
@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
System.out.println(list+"============");
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "entityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource())
.packages("com.opallios.ode.model")
.persistenceUnit("primary")
.properties(buildProperties())
.build();
}
@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource())
.packages("com.opallios.ode.model")
.persistenceUnit("secondary")
.properties(buildProperties())
.build();
}
private Map<String, Object> buildProperties() {
// This is usually set by HibernateJpaAutoConfiguration
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.ejb.naming_strategy",
ImprovedNamingStrategy.class.getName());
return properties;
}
@Bean
@Autowired
@Qualifier("secondaryEntityManagerFactory")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(emf);
return txManager;
}
@Bean
@Autowired
@Qualifier("entityManagerFactory")
public PlatformTransactionManager transactionManagerr(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(emf);
return txManager;
}
@Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
Map<String,String> params = new HashMap<String,String>();
params.put("webAllowOthers", h2WebAllow);
registration.setInitParameters(params);
registration.addUrlMappings("/console/*");
return registration;
}enter code here
它给予例外 org.springframework.beans.factory.BeanCreationException:创建名为&#39; springSecurityFilterChain
的bean时出错我想要所有的建议,如果我需要一个简单的代码,其中可以配置mysql和h2 db并可以从db获取数据。