我在春季启动时遇到了一个初始化Jedis池子类bean的异常

时间:2018-07-13 07:19:19

标签: java spring jedis

我想在Spring boot(1.5.14)中初始化Jedis池bean,如果配置了sentinel,它应该返回JedisSentinelPool;否则返回JedisPool

所以我的代码在这里:

@Resource
private RedisProperties redisProperties;

@Bean(destroyMethod = "close")
public Pool<Jedis> jedisPool(JedisConnectionFactory conn) {
    if (conn.isRedisSentinelAware()) {
        return getJedisSentinelPool(conn);
    } else {
        return getJedisPool(conn);
    }
}

private JedisPool getJedisPool(JedisConnectionFactory conn) {
    if (StringUtils.isEmpty(conn.getPassword())) {
        return new JedisPool(conn.getPoolConfig(), conn.getHostName(), conn.getPort(), conn.getTimeout());
    } else {
        return new JedisPool(conn.getPoolConfig(), conn.getHostName(), conn.getPort(), conn.getTimeout(), conn.getPassword());
    }
}

private JedisSentinelPool getJedisSentinelPool(JedisConnectionFactory conn) {
    Set<String> sentinels = new HashSet<>(asList(redisProperties.getSentinel().getNodes().split(",")));
    if (StringUtils.isEmpty(conn.getPassword())) {
        return new JedisSentinelPool(redisProperties.getSentinel().getMaster(), sentinels, conn.getPoolConfig(), conn.getTimeout());
    } else {
        return new JedisSentinelPool(redisProperties.getSentinel().getMaster(), sentinels, conn.getPoolConfig(), conn.getTimeout(), conn.getPassword());
    }
}

返回JedisPool时,一切都很好;但是如果返回JedisSentinelPool,则会出现此异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.imlht.springboot.dubbo.provider.RedisTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'jedisPool' is expected to be of type 'redis.clients.jedis.JedisPool' but was actually of type 'redis.clients.jedis.JedisSentinelPool'

    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1272)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'jedisPool' is expected to be of type 'redis.clients.jedis.JedisPool' but was actually of type 'redis.clients.jedis.JedisSentinelPool'
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:384)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:522)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
    ... 26 more

JedisSentinelPoolJedisPool都是Pool的子类,为什么JedisSentinelPool有这样的例外?我该如何解决?非常感谢!

0 个答案:

没有答案