如何在运行时

时间:2016-07-15 19:24:50

标签: spring-boot spring-session

我使用Spring Boot 1.3.6和Spring Session以及Redis作为Session存储。我需要能够为应用程序设置redisNamespace运行时,而不是在代码中对其进行硬编码。这意味着我无法使用@EnableRedisHttpSession,因为这不是我可以通过applaction.yml文件设置的内容。

我已将Spring Session更新为1.2.1.RELEASE以获得我需要的支持,但我似乎无法通过配置让系统配置命名空间。我尝试在@EnableRedisHttpSession(redisNamespace)调用中使用SPEL,但这不起作用。我试图通过RedisHttpSessionConfiguration注入Autowired来设置它,这似乎被忽略了。在另一篇SO帖子中还有另一篇关于在spring.session.redis.namespace中使用application.yml作为属性的内容,但这不起作用。

任何建议/提示都将不胜感激!

2 个答案:

答案 0 :(得分:1)

我只是将-Dspring.session.redis.namespace=myKeyName添加到VM参数中。 它工作正常。

enter image description here

我正在使用spring boot v1.3.5.RELEASE和spring session 1.2.0.RELEASE。

答案 1 :(得分:0)

仅按the documentation声明spring.redis.namespace是不够的。

在将@EnableRedisHttpSession()设置为@EnableRedisHttpSession(redisNamespace = "${spring.redis.namespace}")之后,它可以工作了。 根据{{​​3}}的常规功能,您也可以使用系统环境变量代替spring.redis.namespace

注意:在这种情况下,spring.redis.namespace对应于application.yaml文件,如下所示:

  spring:
    redis:
      namespace: ${REDIS_NAMESPACE:foobar}