0

Not able to access the properties while using Spring-session with Redis. Auto wiring is not happening hence this object is null. Not sure what wrong I'm doing here.

@Autowired private RedisSentinelProperties redisSentinelProperties;

Without spring-session it works fine without any issue.

I have tried without spring-session and it works fine. Able to access the all the properties and Auto wiring happens properly

@Autowired private RedisSentinelProperties redisSentinelProperties;

Custom properties configuration

@Component
@ConfigurationProperties(prefix = "app.redis")
@Validated
public class RedisSentinelProperties {
    @NotNull
    private String masterName;
    @Valid
    private Sentinel sentinel = new Sentinel();
    ////removed the getter and setter method for better readability

    public static class Sentinel {
        @NotEmpty
        private List<String> nodes = new ArrayList<>();

        //removed the getter and setter method for better readability
    }
}

application.properties

app.redis.master-name=mymaster
app.redis.sentinel.nodes=192.168.56.50:26379,192.168.56.50:26380,192.168.56.50:26381

spring-session configuration

@SpringBootApplication
public class ConfigPropertiesDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigPropertiesDemoApplication.class, args);
    }
}

public class RedisHttpSessionInitializer extends AbstractHttpSessionApplicationInitializer {

    public RedisHttpSessionInitializer() {
        super(RedisHttpSessionConfig.class);
    }
}

@Configuration
@EnableRedisHttpSession
@EnableWebSecurity
public class RedisHttpSessionConfig {

    @Autowired
    private RedisSentinelProperties redisSentinelProperties;

    LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
            .readFrom(SLAVE_PREFERRED)
            .build();

    private Set<String> sentinelHostAndPorts(){
        Set<String> nodes = redisSentinelProperties.getSentinel().getNodes().stream().collect(Collectors.toSet());
        return nodes;
    }

    //This is where NullPointerException is thrown line 37 in the stack trace
    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration(redisSentinelProperties.getMasterName(), sentinelHostAndPorts());

    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory(sentinelConfig, clientConfig);
    }
}

Below is the stack trace

Caused by: java.lang.NullPointerException at com.bt.consumer.configpropertiesdemo.config.RedisHttpSessionConfig.(RedisHttpSessionConfig.java:37) at com.bt.consumer.configpropertiesdemo.config.RedisHttpSessionConfig$$EnhancerBySpringCGLIB$$f6d40824.() at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)

Nandeesh
  • 107
  • 5
  • 14
  • I tried using Constructor injection. `public RedisHttpSessionConfig(RedisSentinelProperties redisSentinelProperties){ this.redisSentinelProperties = redisSentinelProperties; }` with this change I can see the object RedisSentinelProperties with all the property details but still I get NullPointerException – Nandeesh May 09 '19 at 11:36
  • I made it work. The issue was the way the RedisSentinelConfiguration was created. With the below approach it works:: `public RedisSentinelConfiguration sentinelConfig() { return new RedisSentinelConfiguration(redisMasterName(), sentinelHostAndPorts()); }` – Nandeesh May 09 '19 at 13:14
  • can anyone tell why the above approach works? – Nandeesh May 09 '19 at 13:34

0 Answers0