在xml格式中,您可以定义一些代码,以便设置两个数据源而不会出现问题:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<bean id="dataSource" class="com.zangland.routing.RoutingDataSource">
<property name="targetDataSources">
<map key-type="com.zangland.routing.DbType">
<entry key="MASTER" value-ref="dataSourceMaster"/>
<entry key="REPLICA" value-ref="dataSourceReplica"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceMaster"/>
</bean>
如果我尝试在这样的java中转换它,它会产生错误
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "masterEntityManager", transactionManagerRef = "masterTransactionManager")
public class DataSourceConfiguration {
@Bean(name = "masterEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] {"com.zangland"});
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalJpaProperties());
em.setPersistenceUnitName("master");
return em;
}
@Bean(name = "masterTransactionManager")
public JpaTransactionManager transactionManager(EntityManagerFactory masterEntityManager){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(masterEntityManager);
return transactionManager;
}
@Bean
public RoutingDataSource dataSource() {
RoutingDataSource dataSource = new RoutingDataSource();
Map<Object, Object> dataSourceMap = new HashMap<Object, Object>();
dataSourceMap.put(DbType.MASTER, dataSourceMaster());
dataSourceMap.put(DbType.REPLICA, dataSourceReplica());
dataSource.setTargetDataSources(dataSourceMap);
dataSource.setDefaultTargetDataSource(dataSourceMaster());
dataSource.afterPropertiesSet();
return dataSource;
}
,错误如下:
org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; masterEntityManager&#39;的bean在类路径中定义 resource [com / zangland / routing / DataSourceConfiguration.class]:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例 [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: 工厂方法&#39; entityManagerFactory&#39;抛出异常;嵌套 异常是org.springframework.beans.factory.BeanCreationException: 创建名称为&#39; dataSource&#39;的bean时出错在类路径中定义 resource [com / zangland / routing / DataSourceConfiguration.class]:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[com.zangland.routing.RoutingDataSource]:工厂方法 &#39;数据源&#39;抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; dataSourceMaster&#39;的bean在类路径中定义 资源[com / zangland / routing / DataSourceConfiguration.class]: bean的初始化失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用名称&#39; dataSourceInitializer创建bean&#39 ;: init的调用 方法失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; routingDataSource&#39;的bean在文件中定义 [C:\工作空间\路由数据springboot \目标\类\ COM \ zangland \路由\ RoutingDataSource.class]: 调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException:Property&#39; targetDataSources&#39;是 需要