Spring:在注释中注入属性值

问题描述 投票:0回答:1

我有以下课程:

@EnableRedisIndexedHttpSession(maxInactiveIntervalInSeconds = 2000)
public class MyApplication {

我想输入

maxInactiveIntervalInSeconds
Spring 属性文件中的值(而不是硬编码的
2000
)。我不能在那里使用@Value。

有什么想法吗?

提前致谢!

spring spring-boot spring-cache
1个回答
0
投票

正如文件所说

更高级的配置可以扩展 改为 RedisIndexedHttpSessionConfiguration。

尝试创建自己的配置来覆盖

RedisIndexedHttpSessionConfiguration

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MyRedisSessionProperties.class)
public class CustomRedisIndexedHttpSessionConfiguration extends RedisIndexedHttpSessionConfiguration {

    private final MyRedisSessionProperties myRedisSessionProperties;

    @Autowired
    void customize(MyRedisSessionProperties properties) {
        setMaxInactiveIntervalInSeconds((int) properties.getMaxInactiveInterval().getSeconds());
    }

    @ConfigurationProperties("my.redis.session")
    static class MyRedisSessionProperties {

        private Duration maxInactiveInterval;

        public Duration getMaxInactiveInterval() {
            return this.maxInactiveInterval;
        }

        public void setMaxInactiveInterval(Duration maxInactiveInterval) {
            this.maxInactiveInterval = maxInactiveInterval;
        }

    }
}

应用程序.属性

my.redis.session.max-inactive-interval=1h
© www.soinside.com 2019 - 2024. All rights reserved.