How to choose Session Store depending on active Profile in Spring Boot?

120 Views Asked by At

In a Spring Boot 3.2 application I want to choose which session store is being used, depending on active profiles.

In older Spring Boot versions there was a property to set:

spring.session.store-type=jdbc

So if you want to use redis instead, just override this value in the depending profile properties.

However, this property is no longer available. Since I can't think that Spring Boot creators wants to make life harder for us developers, I'm wondering what the current way to go is.

Spring Session by default has now an order for choosing a specific implementation when there are more than one session implementations available.

My question is: How can I tell Spring Boot which session management to use depending on an active profile?

1

There are 1 best solutions below

4
Ruslan Zinovyev On

You can try to provide the configuration explicitly. Something like this:

@Configuration
@Profile("prod")
@EnableJdbcHttpSession
public class JdbcSessionConfig {
    // JDBC session configuration
}

@Configuration
@Profile("test")
@EnableRedisHttpSession
public class RedisSessionConfig {
    // Redis session configuration
}