Ошибка экземпляра bean-компонента JedisConnectionFactory во время запуска приложения и выдает java.lang.NullPointerException - PullRequest
0 голосов
/ 11 января 2019

Я использую данные весенней загрузки, redis и jedis. Я создал bean-компоненты jedisConnectionFactory и redisTemplate в классе конфигурации. Ошибка создания экземпляра компонента JedisConnectionFactory во время запуска приложения.

Я использую последние библиотеки. Это исключение, которое я получаю:

[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 Ответ

0 голосов
/ 13 января 2019

Из просмотра кода JedisConnectionFactory кажется, что вы можете получить такую ​​ошибку, только если один из аргументов Ctor равен нулю.

    public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig) {

        this(clientConfig);

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

        this.standaloneConfig = standaloneConfig;
    }
...