我正在尝试使用两个数据源设置Spring Boot应用程序。我已经成功地获得了第一个数据源,但是当我添加类来处理第二个数据源时,第一个数据源不再起作用并导致第一个数据源没有自动装配的DAO,这意味着DOA可以&# 39;加载到我的控制器中。
这是设置第一个数据源的类。
@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(
basePackages = "digital.sheppard.dao.local",
entityManagerFactoryRef = "localEntityManager",
transactionManagerRef = "localTransactionManager"
)
public class LocalDBConfig {
@Autowired
private Environment env;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean localEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(localDataSource());
em.setPackagesToScan(new String[] { "digital.sheppard.model.local" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("datasource.local.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("datasource.local.hibernate.dialect"));
properties.put("hibernate.naming-strategy", env.getProperty("org.hibernate.cfg.ImprovedNamingStrategy"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
public DataSource localDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("datasource.local.driver-class-name"));
dataSource.setUrl(env.getProperty("datasource.local.url"));
dataSource.setUsername(env.getProperty("datasource.local.username"));
dataSource.setPassword(env.getProperty("datasource.local.password"));
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager localTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(localEntityManager().getObject());
return transactionManager;
}
}
这是尝试设置第二个数据源的类。
@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(
basePackages = "digital.sheppard.dao.eclipse",
entityManagerFactoryRef = "eclipseEntityManager",
transactionManagerRef = "eclipseTransactionManager"
)
public class EclipseDBConfig {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean eclipseEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(eclipseDataSource());
em.setPackagesToScan(new String[] { "digital.sheppard.model.eclipse" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("datasource.eclipse.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("datasource.eclipse.hibernate.dialect"));
properties.put("hibernate.naming-strategy", env.getProperty("org.hibernate.cfg.ImprovedNamingStrategy"));
em.setJpaPropertyMap(properties);
return em;
}
public DataSource eclipseDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("datasource.eclipse.driver-class-name"));
dataSource.setUrl(env.getProperty("datasource.eclipse.url"));
dataSource.setUsername(env.getProperty("datasource.eclipse.username"));
dataSource.setPassword(env.getProperty("datasource.eclipse.password"));
return dataSource;
}
@Bean
public PlatformTransactionManager eclipseTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(eclipseEntityManager().getObject());
return transactionManager;
}
}
在我看来,第二个数据源正在覆盖第一个数据源,因此应用程序不再能够找到主数据源中定义的DOA。
为什么我的第二个DataSource会阻止DAO从第一个DataSource再到不再加载?