0

Trying to configure spring boot application with spring-session and redis but having below issue. Not able to resolve it.

Constructor threw exception; nested exception is java.lang.IllegalStateException: BeanFactory has not been injected into @Configuration class

This code works fine for me

@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {   

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

    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
            .master("mymaster")
            .sentinel("192.168.56.50", 26379)
            .sentinel("192.168.56.50", 26380)
            .sentinel("192.168.56.50", 26381);

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

but not this code using PropertySource.

Spring document says:- **RedisSentinelConfiguration can also be defined with a PropertySource, which lets you set the following properties:

Configuration Properties spring.redis.sentinel.master: name of the master node.

spring.redis.sentinel.nodes: Comma delimited list of host:port pairs.**

@Configuration
@EnableRedisHttpSession
@PropertySource(name="application", value="classpath:application.properties")
public class HttpSessionConfig {

    @Resource
    ConfigurableEnvironment environment;

    @Bean
    public PropertiesPropertySource propertySource() {
        return (PropertiesPropertySource) environment.getPropertySources().get("defaultProperties");
    }

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

    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration(propertySource());

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

}

application.properties

server.port=8090
spring.security.user.name=admin
spring.security.user.password=admin
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.56.50:26379,192.168.56.50:26380,192.168.56.50:26381
spring.application.name=spring-session-demo
Nandeesh
  • 107
  • 5
  • 14
  • I made it work with the below approach `private PropertySource propertySource() { Optional> propertySource = ((AbstractEnvironment) environment).getPropertySources().stream().filter(ps -> ps instanceof OriginTrackedMapPropertySource).findFirst(); return (PropertySource)propertySource.get(); }`. Not sure if there is any elegant approach – Nandeesh May 09 '19 at 14:17

1 Answers1

0

The format of sentinel nodes property is comma separated key:value pairs. So you can extract host and port by java split() function.

@Autowired
private Environment env;

@Bean
public LettuceConnectionFactory connectionFactory() {

    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration();   

    String master = env.getProperty("spring.redis.sentinel.master");
    String nodes = env.getProperty("spring.redis.sentinel.nodes");

    sentinelConfig.master(master);
    for (String node : nodes.split(",")) {
        String split[] = node.split(":");
        sentinelConfig.sentinel(split[0], Integer.parseInt(split[1]));
    }

    ...

}
shizhen
  • 12,251
  • 9
  • 52
  • 88
ari
  • 1