【问题标题】:why SpringBoot not Creating bean for mulitple datasources?为什么 Spring Boot 不为多个数据源创建 bean?
【发布时间】:2017-02-17 07:31:43
【问题描述】:

我想在应用程序中使用多个数据源(oracle 和 sqlserver)。 据此,我已经完成了配置。 我写的代码就像

配置文件

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.secondDasource")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(firstDataSource());
    }

    @Bean
    public JdbcTemplate sJdbcTemplate() {
        return new JdbcTemplate(secondDataSource());
    }
}

application.yml 文件

#First                                                                                                                              
spring.datasource.driverClassName: oracle.jdbc.driver.OracleDriver                                                                  
spring.datasource.url: someJdbcUrl                                                                                                  
spring.datasource.username: username                                                                                                
spring.datasource.password: password                                                                                                

#Second                                                                                                                             
spring.secondDatasource.driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver                                               
spring.secondDatasource.url: some sqlUrl                                                                                            
spring.secondDatasource.username: username                                                                                          
spring.secondDatasource.password: password     

主类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class MainApp {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainApp.class, args);
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(MainApp.class, args);
        configurableApplicationContext.start();

        String[] beanNames = configurableApplicationContext.getBeanDefinitionNames();
        System.out.println("--------------------------------------");
        System.out.println();

        for (String beanName : beanNames) {

            System.out
                    .println(beanName + " : " + configurableApplicationContext.getBean(beanName).getClass().toString());
        }

        System.out.println("--------------------------------------");
        System.out.println();

    }
}

gradle 依赖

        compile group: 'sqljdbc4', name: 'sqljdbc4', version: '4.0'
        compile(group: 'ojdbc14', name: 'ojdbc14', version: '10.2.0.5')

当我尝试使用第二个数据源时,它会抛出异常,因为它的 bean 尚未创建

我在这里缺少什么??

【问题讨论】:

标签: java gradle spring-boot jdbctemplate


【解决方案1】:

尝试通过 bean 名称进行注入:

        @Bean
        public JdbcTemplate jdbcTemplate(@Qualifier("firstDataSource") DataSource firstDataSource){
            return new JdbcTemplate(firstDataSource);
        }

        @Bean
        public JdbcTemplate sJdbcTemplate(@Qualifier("secondDataSource") DataSource secondDataSource){
            return new JdbcTemplate(secondDataSource);
        }

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2019-07-13
    • 1970-01-01
    • 2021-07-17
    • 2019-07-30
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多