I have the following class:
@EnableRedisIndexedHttpSession(maxInactiveIntervalInSeconds = 2000)
public class MyApplication {
I want to put in maxInactiveIntervalInSeconds a value from the Spring properties file (instead of the hardcoded 2000). I can't use @Value there.
Any idea?
Thanks in advance!
It is technically much simpler than this, even.
I assume you are using Spring Boot, so we will refer to
3.2.0-RC1Spring bits (both Spring Boot and Spring Session). Earlier versions, especially in the3.xline, should functionally be the same.You certainly don't want to be in the habit of directly extending the Spring Session for Redis,
RedisIndexedHttpSessionConfigurationclass (Javadoc), partly because this is precisely the class that the@EnableRedisIndexedHttpSessionannotation "imports" (Javadoc), and both Spring Session as well as Spring Boot make "customization" easy.SPRING BOOT WAY:
You can simply set the following property in your Spring Boot
application.properties:See Spring Boot documentation on the
spring.session.timeoutproperty.The fallback property used by Spring Boot is:
server.servlet.session.timeoutThis is evident from the Spring Boot auto-configuration (source).
SPRING SESSION WAY (without Spring Boot):
It is even simple when you are not using Spring Boot. Simply register a bean of type
SessionRepositoryCustomizer(Javadoc) from Spring Session, like so:Then, your
my.session.timeoutproperty can be configured from any location that Spring'sEnvironmentsupports as a "property source", including, but not limited to: Java System properties, properties files, Environment variables, and so on, including your own custom implementations as necessary, not unlike what Spring Boot provides for you already as well.