使用spring配置多个数据源,包括namedparameterjdbctemplate

时间:2018-07-31 12:25:52

标签: java spring spring-jdbc jdbctemplate

我的项目中有以下数据库配置类,其中我尝试为两个数据源之一配置多个jdbctemplate和一个命名的jdbc模板:

@Configuration
public class DatabaseConfiguration {

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

@Bean(name = "jdbcABC")
@Autowired
public JdbcTemplate abcJdbcTemplate(@Qualifier("abcDataSource") DataSource datasource) {
    return new JdbcTemplate(datasource);
}

@Bean(name = "namedJdbcABC")
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("abcDataSource") DataSource datasource) {
    return new NamedParameterJdbcTemplate(datasource);
}


@Bean(name = "shDataSource")
@ConfigurationProperties(prefix = "spring.shdatasource")
public DataSource shDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "jdbcSh")
@Autowired
public JdbcTemplate shJdbcTemplate(@Qualifier("shDataSource") DataSource datasource) {
    return new JdbcTemplate(datasource);
}

我似乎已经在yaml文件中正确配置了所有内容:

spring:
    profiles:
        active: dev
    output.ansi.enabled: always

    shdatasource:
        driverClassName: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@sh.database.url:1521:sid
        username: test
        password: test
    datasource:
        driverClassName: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@abc.db.url:1521:oll
        username: test
        password: test

但是我得到了错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'somedao': Unsatisfied dependency expressed through field 'namedParameterJdbcTemplate';

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'abcDataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found

任何人都可以告诉我是否配置错误?

1 个答案:

答案 0 :(得分:0)

这样做对我有用:

@Configuration
public class DatabaseConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties abcDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "abcDataSource")
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSource abcDataSource() {
        return abcDataSourceProperties().initializeDataSourceBuilder().build();
    }


    @Bean
    @ConfigurationProperties("spring.shDataSource")
    public DataSourceProperties shDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "shDataSource")
    @ConfigurationProperties("spring.shDataSource")
    public DataSource shDataSource() {
        return shDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean(name = "jdbcABC")
    @DependsOn("abcDataSource")
    public JdbcTemplate abcJdbcTemplate(@Qualifier("abcDataSource") DataSource abcDataSource) {
        return new JdbcTemplate(abcDataSource);
    }


    @Bean(name = "namedJdbcABC")
    @DependsOn("abcDataSource")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("abcDataSource") DataSource abcDataSource) {
        return new NamedParameterJdbcTemplate(abcDataSource);
    }

    @Bean(name = "jdbcSh")
    @DependsOn("shDataSource")
    public JdbcTemplate shJdbcTemplate(@Qualifier("shDataSource") DataSource shDataSource) {
        return new JdbcTemplate(shDataSource);
    }

}

然后,在我的主班上,我注入了豆子:

@SpringBootApplication
public class Application implements CommandLineRunner {    


    @Autowired
    @Qualifier("abcDataSource")
    DataSource abcDataSource;

    @Autowired
    @Qualifier("shDataSource")
    DataSource shDataSource;

    @Autowired
    @Qualifier("jdbcABC")
    JdbcTemplate abcJdbcTemplate;

    @Autowired
    @Qualifier("namedJdbcABC")
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    @Qualifier("jdbcSh")
    JdbcTemplate shJdbcTemplate;


    @Override
    public void run(String... strings) throws Exception {
        System.out.println(abcDataSource.toString());
        System.out.println(shDataSource.toString());
        System.out.println(abcJdbcTemplate.toString());
        System.out.println(namedParameterJdbcTemplate.toString());
        System.out.println(shJdbcTemplate.toString());
    }


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

它按预期工作。