1

I have a dependency on my custom Redis starter that has a configuration class, Which I want to start before RedissonAutoConfiguration, for that, I use @AutoConfigureBefore annotation, though, it seems not to work and still, RedissonAutoConfiguration is started first.

The only thing to work seems is adding @ComponentScan(basePackage = "path_to_config"), but I do not want to use any annotation here, I want started to start before RedissonAutoConfiguration by itself

I have spring.factories file in my Redis starter to make the class autoconfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
za.co.discovery.health.dhp.starter.redis.config.RedisClientConfiguration
package za.co.discovery.health.dhp.starter.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.List;


@Configuration
@AutoConfigureBefore(RedissonAutoConfiguration.class)
public class RedisClientConfiguration {

    @Profile("single-server-redis-client")
    @Bean(destroyMethod = "shutdown")
    public RedissonClient singleServerRedissonClient(@Value("${single-server.redis.client.host:localhost}") String redisClientHost, @Value("${single-server.redis.client.port:6379}") long redisClientPort) {
        Config config = new Config();
        config.useSingleServer().setAddress(String.format("redis://%s:%d", redisClientHost, redisClientPort));
        return Redisson.create(config);
    }

    @Profile("cluster-redis-client")
    @Bean(destroyMethod = "shutdown")
    public RedissonClient clusterRedissonClient(@Value("${spring.redis.sentinel.nodes}") List<String> sentinelNodes, @Value("${spring.redis.sentinel.master}") String sentinelMaster, @Value("${spring.redis.sentinel.password}") String password) {
        Config config = new Config();
        config.useSentinelServers().setMasterName(sentinelMaster).setPassword(password);
        sentinelNodes.forEach((sentinelNode) -> {
            config.useSentinelServers().addSentinelAddress("redis://" + sentinelNode);
        });
        return Redisson.create(config);
    }

}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Do you actually start your application with one of the profiles? And is the `spring.factories` in the correct location? Is it actually in the jar you created? – M. Deinum Feb 24 '21 at 07:57
  • What do you mean when you say " RedissonAutoConfiguration is started first"? Auto-configured ordering controls the order in which the beans are defined. It does not control the order in which the beans are created. – Andy Wilkinson Feb 24 '21 at 11:09
  • I do have spring.factories file – Tornike Kechakmadze Mar 10 '21 at 18:17
  • Hey Andy, What I mean is that spring boot firstly tries to create reddisson bean defined in RedisAutoConfiguration.java class. I want to reverse it. – Tornike Kechakmadze Mar 10 '21 at 18:19

0 Answers0