尝试连接生菜连接工厂时出错

时间:2014-11-12 18:26:03

标签: java spring spring-data spring-data-redis apache-commons-pool

我正在尝试使用Spring Data Redis(版本:1.1.x)为我的redis客户端(生菜客户端版本:2.3.3)设置连接工厂,但是在将所有内容连接在一起时遇到了一些麻烦。< / p>

这是我的配置:application-context-redis.xml

<bean id="lettuceConnectionFactory"
      class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
    <constructor-arg index="0" ref="redisPool"/>
</bean>

<bean id="redisPool"
      class="org.springframework.data.redis.connection.lettuce.DefaultLettucePool">
        <constructor-arg index="0" value="${redis.host}" />
        <constructor-arg index="1" value="${redis.port}"/>
        <constructor-arg index="2" ref="#{redisPoolConfig.Config}"/>
</bean>

<bean id="redisPoolConfig" class="org.apache.commons.pool.impl.GenericObjectPool"
      p:maxActive="${redis.maxActive}"
      p:maxIdle="${redis.maxIdle}"
      p:minIdle="${redis.minIdle}"
      p:maxWait="${redis.maxWait}"
      p:testOnBorrow="${redis.testOnBorrow}"
      p:testOnReturn="${redis.testOnReturn}"
      p:testWhileIdle="${redis.testWhileIdle}"
      p:whenExhaustedAction="${redis.whenExhaustedAction}"
      p:timeBetweenEvictionRunsMillis="${redis.timeBetweenEvictionRunsMillis}"
      p:numTestsPerEvictionRun="${redis.numTestsPerEvictionRun}"
      p:softMinEvictableIdleTimeMillis="${redis.softMinEvictableIdleTimeMillis}"
      p:minEvictableIdleTimeMillis="${redis.minEvictableIdleTimeMillis}"/>
/>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
      p:connectionFactory-ref="lettuceConnectionFactory"/>

这是 redisPool bean的构造函数签名:

public DefaultLettucePool(String hostName, int port, Config poolConfig) {
        this.hostName = hostName;
        this.port = port;
        this.poolConfig = poolConfig;
}

poolConfig 属性是一个公共内部静态类:org.apache.commons.pool.impl.GenericObjectPool.Config

由于 配置 属性is a static inner class (see here)并且不包含getter方法,因此我很难将其注入 < em> redisPool bean。

这是我得到的错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisPool' defined in class path resource [application-context-redis.xml]: Cannot resolve reference to bean '#{redisPoolConfig.Config}' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 16): Field or property 'Config' cannot be found on object of type 'org.apache.commons.pool.impl.GenericObjectPool'
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:615)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1045)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:949)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
    ... 72 more
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 16): Field or property 'Config' cannot be found on object of type 'org.apache.commons.pool.impl.GenericObjectPool'
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142)
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1312)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:225)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:312)
    ... 84 more

1 个答案:

答案 0 :(得分:1)

Spring Data Redis附带了PoolConfig的{​​{1}}包装,它基本上允许您解决上述问题。

org.apache.commons.pool.impl.GenericObjectPool.Config