JedisConnectionFactory

JedisConnectionFactory

我使用的是SpringBoot数据、Redis和绝地。我在一个配置类中创建了jedisconnectionfactory和redistemplate bean。应用程序启动期间,jedisconnectionfactory bean实例化失败。
我用最新的图书馆。这是我得到的例外:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-
 maven-plugin:2.2.0.BUILD-SNAPSHOT:run (default-cli) on project
Console: An exception occurred while running. null:
InvocationTargetException: Error creating bean with name
'consoleApplication': Unsatisfied dependency expressed through field
'bookRepository'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'bookRepository': Cannot resolve reference
to bean 'redisKeyValueTemplate' while setting bean property
'keyValueOperations'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'redisKeyValueTemplate': Cannot resolve
reference to bean 'redisKeyValueAdapter' while setting constructor
argument; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'redisKeyValueAdapter': Cannot resolve
reference to bean 'redisTemplate' while setting constructor
argument; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'redisTemplate' defined in class path
resource [com/console/config/AppConfig.class]: Bean instantiation
via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.springframework.data.redis.core.RedisTemplate]:
Factory method 'redisTemplate' threw exception; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'jedisConnectionFactory' defined in class
path resource [com/console/config/AppConfig.class]: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'jedisConnectionFactory' threw exception; nested exception is java.lang.NullPointerException -> [Help 1]

这是我的pom.xml:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
   <!-- <version>2.2.0.BUILD-SNAPSHOT</version> -->
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <!-- <version>3.0.1</version> -->
    <!-- <type>jar</type> -->
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

这是我的代码:
@Configuration
public class AppConfig {
    @Autowired
    private RedisProperties redisProperties;
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());redisStandaloneConfiguration.setPassword(redisProperties.getPassword());
         JedisClientConfiguration.JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder()
            .connectTimeout(redisProperties.getTimeout())
    .readTimeout(redisProperties.getJedis().getPool().getMaxWait());
    if (redisProperties.isSsl())
        builder.useSsl();
    // Final JedisClientConfiguration
    JedisClientConfiguration clientConfig = builder.build();//.usePooling().build();
    return new JedisConnectionFactory(redisStandaloneConfiguration, clientConfig);
    }
    @Bean
    public RedisTemplate<Object, Object> redisTemplate() {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        JedisConnectionFactory factory = jedisConnectionFactory();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}
@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
}

我希望这个简单的应用程序能够毫无例外地运行。
任何建议和见解都是值得赞赏的。

最佳答案

通过查看jedisconnectionfactory代码,似乎只有当ctor的一个参数为空时,您才能得到这样的错误。

    public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig) {

        this(clientConfig);

        Assert.notNull(standaloneConfig, "RedisStandaloneConfiguration must not be null!");

        this.standaloneConfig = standaloneConfig;
    }

09-15 21:05