1

我依赖于我的自定义 Redis 启动器,它有一个配置类,我想在此之前启动RedissonAutoConfiguration它,为此,我使用@AutoConfigureBefore注释,但它似乎不起作用,仍然RedissonAutoConfiguration是首先启动的。

唯一可以工作的似乎是添加@ComponentScan(basePackage = "path_to_config"),但我不想在这里使用任何注释,我想自己开始RedissonAutoConfiguration之前

spring.factories我的 Redis 启动器中有文件来进行类自动配置

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);
    }

}
4

0 回答 0