gpt4 book ai didi

spring - 在 Spring 中使用多个数据源

转载 作者:行者123 更新时间:2023-12-01 23:54:01 25 4
gpt4 key购买 nike

我正在开发一个使用 Spring Data JPA 的 Spring Boot 应用程序,需要连接到两个不同的数据库,我遇到了一个问题。当应用程序启动时,它抛出一个异常,基本上表明它不知道要使用哪个数据源。这是我的配置类的一个片段。数据库属性(例如 url、用户名、密码)在 application.properties 文件中定义。

@Configuration
@ComponentScan(basePackages = {"com.test"})
@EnableJpaRepositories(basePackages = {"com.test.jpa.repository"})
@EnableAutoConfiguration
@EnableTransactionManagement
public class AppConfig {

@Bean(name = "dataSource1")
DataSource dataSource1(@Value("${db1.driverClassName}") String db1DriverClassName, @Value("${db1.url}") String db1Url, @Value("${db1.username}") String db1Username, @Value("${db1.password}") String db1Password) {
DataSource dataSource = new DataSource();

dataSource.setDriverClassName(db1DriverClassName);
dataSource.setUrl(db1Url);
dataSource.setUsername(db1Username);
dataSource.setPassword(db1Password);

return dataSource;
}

@Bean(name = "dataSource2")
DataSource dataSource2(@Value("${db2.driverClassName}") String db2DriverClassName, @Value("${db2.url}") String db2Url, @Value("${db2.username}") String db2Username, @Value("${db2.password}") String db2Password) {
DataSource dataSource = new DataSource();

dataSource.setDriverClassName(db2DriverClassName);
dataSource.setUrl(db2Url);
dataSource.setUsername(db2Username);
dataSource.setPassword(db2Password);

return dataSource;
}

@Bean(name = "jpaVendorAdapter")
HibernateJpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();

jpaVendorAdapter.setDatabase(Database.ORACLE);
jpaVendorAdapter.setShowSql(false);
jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle10gDialect");

return jpaVendorAdapter;
}

@Bean(name = "entityManagerFactory")
@Autowired
LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("dataSource1") DataSource dataSource, HibernateJpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPersistenceXmlLocation("classpath:persistence.xml");
entityManagerFactory.setPersistenceUnitName("springdatajpa");
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);

return entityManagerFactory;
}

@Bean(name = "transactionManager")
JpaTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());

return transactionManager;
}

这是抛出的异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.dataSource; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: dataSource1,dataSource2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
at com.wth.service.logging.Application.main(Application.java:11)

我的 entityManagerFactory 方法使用 @Autowired 注释进行注释,dataSource 参数前面带有 @Qualifier("dataSource1 ") 注释,所以这应该可以工作。我以前在其他 Spring 应用程序中这样做过,没有遇到任何问题。这是我第一次遇到这种异常。

有没有人对可能导致这种情况的原因有任何想法/想法?

谢谢,克里斯

最佳答案

为了让这个工作正常,我使用了以下内容:

@SpringBootApplication(exclude = { 
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
public class App...

这相当于:

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})

然后我为每个数据源定义了一个单独的配置类,其定义如下:

@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef="fooEntityManagerFactory",
transactionManagerRef="fooTransactionManager")
public class FooConfig {

public static final String FOO_QUALIFIER = "Foo";

@Bean @Qualifier(FOO_QUALIFIER)
public DataSource fooDataSource() { ... }

@Bean @Qualifier(FOO_QUALIFIER)
public EntityManagerFactory fooEntityManagerFactory() { ... }

@Bean @Qualifier(FOO_QUALIFIER)
public PlatformTransactionManager fooTransactionManager(
@Qualifier(FOO_QUALIFIER) EntityManagerFactory emf) { ... }
}

关于spring - 在 Spring 中使用多个数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com