当前位置: 首页 > 知识库问答 >
问题:

JedisConnectionFactory bean实例化在应用程序启动期间失败,并抛出java.lang.NullPointerException

齐招
2023-03-14

我使用的是spring-boot-data,redis和Jedis。我在一个配置类中创建了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> {
}

共有1个答案

欧阳俊捷
2023-03-14

通过查看JedisConnectionFactory代码,似乎只有当Ctor的一个参数为null时才会出现这样的错误。

    public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig) {

        this(clientConfig);

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

        this.standaloneConfig = standaloneConfig;
    }
 类似资料:
  • 当我运行这个错误时,请帮助我解决这个问题 启动应用程序上下文时出错。要显示条件,请报告启用“调试”后重新运行应用程序。2019-02-11 10:53:55.839错误8804 --- [ restartedMain]o. s. b. d.日志失败分析记者: 应用程序启动失败 说明: com中的字段userDao。实例Spring Security应用程序编程接口。服务UserServiceImp

  • 我是spring的初学者,希望这样做示例项目…我有一个数据库,我想连接到它…我使用了像AutoWired和service这样的注释。但有个问题我解决不了 Controller包中的RegisterationController具有我要调用的服务: 第一个包中的DataManagement类: 第一包中的StudentAccountRepository: 第一包中得学生帐户..引用数据库中的表: M

  • > 出错原因:任务“:bootrun”执行失败。 进程'command'/library/java/javavirtualmachines/jdk1.8.0_60.jdk/contents/home/bin/java''以非零退出值1完成 发生异常:2015-08-23 01:37:57.989 WARN 6186---[main]ationConfigEmbeddedWebApplication

  • 当我在WebSphere8.5.5中启动应用程序时,我得到了一个错误,并且应用程序启动失败,我搜索日志并得到了这个堆栈跟踪。你们知道吗?

  • 问题内容: 我遇到错误了; 问题答案: 错误是 看来您有一个指的是您尚未声明的。 您需要具有以下声明

  • 我正在尝试使用Python Splinter Selenium PhantomJS进行基本测试,但我无法启动phantomjs浏览器。(Chrome 我得到一个错误: 此外,当我跑步时: 我基本上得到了同样的错误。 看起来PhantomJS正在使用远程驱动程序连接到远程服务器,但splinter没有启动selenium(?)PhantomJS要连接到的服务器。我不介意进入splinter(?)的源