如何在Spring(4)上下文中为HSQL配置Hikari CP?

时间:2014-04-29 16:06:20

标签: java jdbc hsqldb spring-4 hikaricp

我想在我的Spring 4.0.3上下文中使用Hikari CP但似乎我错过了一些东西。

我的bean配置如下:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
   <constructor-arg>
     <bean class="com.zaxxer.hikari.HikariConfig">
       <constructor-arg>
         <props>
           <prop key="dataSource.driverClassName">${database.driver}</prop>
           <prop key="dataSource.jdbcUrl">${database.database.jdbc.url}</prop>
           <prop key="dataSource.port">${database.port}</prop>
           <prop key="dataSource.databaseName">${database.name}</prop>
           <prop key="dataSource.user">${database.user}</prop>
           <prop key="dataSource.password">${database.password}</prop>
         </props>
       </constructor-arg>

     </bean>
   </constructor-arg>
</bean>

但我得到一个例外:

Caused by: java.lang.IllegalArgumentException: one of either dataSource or dataSourceClassName must be specified
    at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:655)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:66)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 15 more

我尝试使用HSQL org.hsqldb.jdbc.JDBCDataSource

配置dataSourceClassName 那样:

<prop key="dataSourceClassName">org.hsqldb.jdbc.JDBCDataSource</prop>

也是这样:

<prop key="dataSource.ClassName">org.hsqldb.jdbc.JDBCDataSource</prop>

两次我都遇到以下异常:

Caused by: java.lang.RuntimeException: java.beans.IntrospectionException: Method not found: setPort
    at com.zaxxer.hikari.util.PropertyBeanSetter.setProperty(PropertyBeanSetter.java:109)
    at com.zaxxer.hikari.util.PropertyBeanSetter.setTargetFromProperties(PropertyBeanSetter.java:61)
    at com.zaxxer.hikari.pool.HikariPool.initializeDataSource(HikariPool.java:497)
    ... 23 more
Caused by: java.beans.IntrospectionException: Method not found: setPort
    at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:110)
    at com.zaxxer.hikari.util.PropertyBeanSetter.setProperty(PropertyBeanSetter.java:97)
    ... 25 more

有人可以向我展示适用于HSQL DB Hikari CP Spring 4 bean配置吗?

我对MySql,PG等不感兴趣,因为我知道如何让它们正常工作。但我无法通过HSQL完成任务。

谢谢,

使用的技术:Java8,Spring 4.0.3,HSQL 2.3.2

4 个答案:

答案 0 :(得分:2)

完成工作的一种方法是提供DataSource对象的实例:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
  <constructor-arg>
    <bean class="com.zaxxer.hikari.HikariConfig">
      <property name="dataSource">
        <bean class="org.hsqldb.jdbc.JDBCDataSource">
          <property name="url" value="${database.database.jdbc.url}"/>
          <property name="databaseName" value="${database.name}"/>
          <property name="user" value="${database.user}"/>
          <property name="password" value="${database.password}"/>
        </bean>
      </property>
    </bean>
  </constructor-arg>
</bean>

肯定还有其他解决方案。

HTH,

答案 1 :(得分:2)

您示例中的某些属性不需要前缀&#39; dataSource&#39;如果您使用的是驱动程序类。

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
   <constructor-arg>
     <bean class="com.zaxxer.hikari.HikariConfig">
        <constructor-arg>
           <props>
              <prop key="driverClassName">${database.driver}</prop>
              <prop key="jdbcUrl">${database.database.jdbc.url}</prop>
              <prop key="username">${database.user}</prop>
              <prop key="password">${database.password}</prop>
          </props>
       </constructor-arg>
     </bean>
   </constructor-arg>
</bean>

portdatabaseName可以包含在jdbcUrl中。

答案 2 :(得分:2)

对于纯Java-config解决方案,我使用了以下内容(在具有@Configuration注释的类中并包含在component-scan-path中):

...
@Bean
public DataSource dataSource() {
    return new HikariDataSource(hikariConfig());
}

private HikariConfig hikariConfig() {
    HikariConfig config = new HikariConfig();
    config.setDriverClassName(driverClassName);
    config.setJdbcUrl(jdbcUrl);
    config.setUsername(username);
    config.setPassword(password);
    return config;
}
...

HTH

答案 3 :(得分:1)

Tested under HikariCP 2.3.8 and Hibernate 4.3.8.Final:

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <constructor-arg>
        <props>
            <prop key="dataSourceClassName">org.postgresql.ds.PGSimpleDataSource</prop>
            <prop key="dataSource.user">${database.username}</prop>
            <prop key="dataSource.password">${database.password}</prop>
            <prop key="dataSource.databaseName">${database.databaseName}</prop>
            <prop key="dataSource.serverName">${database.serverName}</prop>
            <prop key="connectionTestQuery">SELECT 1</prop>
        </props>
    </constructor-arg>
</bean>

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
    <constructor-arg ref="hikariConfig" />
</bean>

For the dataSourceClassName, looks at the popular datasource class names table.

ConnectionTestQuery is required for postgresql as per https://github.com/brettwooldridge/HikariCP/issues/225, shouldn't be needed when using a latest jdbc driver version.