【问题标题】:Set Spring Session Redis on production only仅在生产环境中设置 Spring Session Redis
【发布时间】:2019-01-11 06:46:00
【问题描述】:

是否可以仅在 production 配置文件上设置 Spring 会话数据 redis 模块?

我曾尝试使用@Profile,但AbstractHttpSessionApplicationInitializer 在设置profile/environment 之前已初始化,因此会给我一个NoSuchBean 异常

NoSuchBeanDefinitionException: No bean named 'springSessionRepositoryFilter' available

当我尝试使用@Values 注入配置文件时,它返回空值。

@Value("${spring.profiles.active:local}")
private String activeProfile; // result is null

【问题讨论】:

    标签: spring-boot spring-session spring-data-redis spring-profiles


    【解决方案1】:

    我已经使用this article解决了这个问题。

    生产会话配置将是这样的。

    @Configuration
    @ConditionalOnProperty(name = "spring.profiles.active", havingValue = "production")
    @EnableRedisHttpSession
    @ConfigurationProperties(prefix = "redis.db")
    public class ProductionSessionConfig {
    
        @Setter
        private String server;
        @Setter
        private Integer port;
    
        @Bean
        public JedisConnectionFactory connectionFactory() {
            //creates your own connection
            RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(server, port);
            return new JedisConnectionFactory(config);
        }
    
    }
    

    如果您不打算在其他环境中使用spring-session-data-redis。你可以继续。请记住 @ConditionalOnProperty 值。

    并且不要忘记排除会话的自动配置。

    @SpringBootApplication(exclude = {SessionAutoConfiguration.class})
    

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 1970-01-01
      • 2018-06-25
      • 2011-04-14
      • 1970-01-01
      • 2020-11-29
      • 2023-04-09
      • 2015-05-30
      • 2015-04-04
      相关资源
      最近更新 更多