【问题标题】:How to configure the annotaion value with configuration file like .properties/.xml/.yml in spring framework?如何在spring框架中使用.properties/.xml/.yml等配置文件配置注解值?
【发布时间】:2021-11-17 15:53:43
【问题描述】:

特定案例的代码 sn-p

@Configuration
@NewEnableRedisHttpSession(maxInactiveIntervalInSeconds = 900)
@Import({RedisConfiguration.class})

这是一个注解值:maxInactiveIntervalInSeconds = 900

@NewEnableRedisHttpSession(maxInactiveIntervalInSeconds = 900)

我想像这样配置它

@NewEnableRedisHttpSession("${maxInactiveIntervalInSeconds}")

一些配置文件会给出值 maxInactiveIntervalInSeconds = 900

【问题讨论】:

    标签: java spring spring-boot spring-mvc


    【解决方案1】:

    使用

    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = intervalInSeconds)
    

    更具体地满足您的需求

    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 900)
    

    默认值为1800

    参见https://docs.spring.io/spring-session/docs/current/api/index.html?org/springframework/session/data/redis/config/annotation/web/http/EnableRedisHttpSession.html 部分maxInactiveIntervalInSeconds

    根据评论补充。这样做,以防你想从application.properties获得价值

    spring.session.timeout=9000
    
    @Value("${spring.session.timeout}")
    private Integer maxInactiveIntervalInSeconds;
    

    https://stackoverflow.com/a/37440818/3728901

    【讨论】:

    • 感谢您的回复! @EnableRedisHttpSession(maxInactiveIntervalInSeconds = intervalInSeconds) 是否意味着我可以在配置文件中设置 intervalInSeconds ?我知道我可以给maxInactiveIntervalInSeconds 赋值,但我需要在配置文件中设置该值。
    • 是的,您可以在application.properties 或另一个*.properties 文件中设置值。在我编辑的答案中查看更多信息。
    • 酷,我试试看。
    • 您可以将key设置为另一个字符串,例如spring.session.timeoutFooBar=900spring.session.timeoutFooBarZuu=1900;然后在@Value("${spring.session.timeoutFooBarZuu}") 进行相应更改
    • 看来工作,你救了我的日子!谢谢
    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2015-09-06
    • 1970-01-01
    • 2018-06-20
    相关资源
    最近更新 更多