【问题标题】:JedisConnectionFactory bean instantiation failure during application startup and throws java.lang.NullPointerExceptionJedisConnectionFactory bean 在应用程序启动期间实例化失败并抛出 java.lang.NullPointerException
【发布时间】:2019-06-06 06:40:55
【问题描述】:

我正在使用 spring-boot-data、redis 和 jedis。我在 Configuration 类中创建了 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> {
}

我希望这个简单的应用程序能够正常运行。

感谢任何建议和见解。

【问题讨论】:

  • 为什么注释掉spring-data-redis 2.2.0-SNAPSHOT版本?
  • 我都试过了。没有什么不同。
  • 好的,但是如果你想用 Jedis 3.0.1 运行,你需要确保你运行的是 2.2.0
  • 你有没有机会得到空指针全栈?

标签: spring-boot redis nullpointerexception spring-data jedis


【解决方案1】:

从查看 JedisConnectionFactory 代码看来,只有当 Ctor 的参数之一为空时,您才会收到此类错误。

    public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig) {

        this(clientConfig);

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

        this.standaloneConfig = standaloneConfig;
    }

【讨论】:

  • 这如何转化为我的源代码?我的源代码的哪一部分可能是错误的?谢谢。
【解决方案2】:

我遇到了同样的问题。对我来说,问题在于创建 bean 的顺序

jedisConnectionFactory bean 在其任何依赖项之前创建是很重要的。告诉 Spring 这一点的一种方法是使用 @DependsOn 注释。你可以这样做:

@SpringBootApplication
@DependsOn("jedisConnectionFactory")
public class Application extends SpringBootServletInitializer {
    ...
}

可能有更好的方法来解决这个问题!我的建议只是一种解决方法。

【讨论】:

    猜你喜欢
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2010-09-08
    • 2017-04-13
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多