使用Hikari和Postgres在Spring Test中配置2个数据源

时间:2019-01-18 03:57:01

标签: spring-boot datasource hikaricp jdbc-postgres

我目前正在研究将在Spring Boot应用程序中使用的数据源模块。我需要使用postgresql和hikari创建2个数据源。

我试图按照指南https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources进行操作,但是我仍然遇到异常情况

JUNIT班

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppDataSourceConfig.class)
public class PropertyDaoImplTest extends AppBaseDAO implements PropertyDAOTest {

    private static final Logger LOGGER = LogManager.getLogger(PropertyDaoImplTest.class);

    @Test
    public void allPropertiesTest() {
        LOGGER.debug("Getting all properties for test!");
        assertTrue(true);

        List<PropertyEntityTest> items = this.primaryWriteTemplate.query(QUERY_ALL_ACTIVE_PROPERTIES, new PropertyMapper());
        assertNotNull(items);


    }
}
@Configuration
public class AppDataSourceConfig extends HikariConfig {

    private final static Logger LOGGER = LogManager.getLogger(AppDataSourceConfig.class);

    @Bean(name = "dataSourceWriteProperties")
    @Primary
    @ConfigurationProperties(prefix = "primary.write.ds")
    public DataSourceProperties writeDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "primaryWriteDataSource")
    @ConfigurationProperties(prefix = "primary.write.ds.config")
    @Primary
    public HikariDataSource primaryWriteDataSource(){
        LOGGER.debug("Creating primary write datasource for R3App!");
        return writeDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

application.properties

primary.write.ds.jdbc-url=jdbc:postgresql://localhost:5432/propAppdb
primary.write.ds.username=
primary.write.ds.password=
primary.write.ds.config.pool-name=primaryWrite
primary.write.ds.config.maximum-pool-size=50
  

由以下原因引起:org.springframework.beans.BeanInstantiationException:无法实例化[com.zaxxer.hikari.HikariDataSource]:工厂方法“ primaryWriteDataSource”引发了异常;嵌套的异常是org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException:无法确定合适的驱动程序类       在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)〜[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]       在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)〜[spring-beans-5.3.3.RELEASE.jar:5.1.3.RELEASE]       ...省略了66个通用框架   原因:org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException:无法确定合适的驱动程序类       在org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:236)〜[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]       在org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176)〜[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]

0 个答案:

没有答案