Yuki  Yoshida
Yuki Yoshida

Reputation: 1263

How to configure JdbcIndexedSessionRepository on spring.main.web-application-type=none

We have 2 spring-boot(3.3.0) application

web-application

scheduler-application

these applications connect to same database.

When we change to disable cleanup-cron on web-application and to enable cleanup-cron on scheduler-application, but JdbcIndexedSessionRepository was not created.

Because SessionAutoConfiguration is annotated @ConditionalOnWebApplication and not autoconfigured.

How can initialize JdbcIndexedSessionRepository at spring.main.web-application-type=none environment?


Current work-around is

scheduler-application

build.gradle

implementation("org.springframework.session:spring-session-jdbc")
+ implementation("org.springframework.boot:spring-boot-starter-web")

application.properties

+ spring.main.lazy-initialization=false
spring.session.store-type=jdbc
spring.session.timeout=24h
spring.session.jdbc.initialize-schema: never

Upvotes: 0

Views: 303

Answers (2)

Yuki  Yoshida
Yuki Yoshida

Reputation: 1263

refer to @AndyWilkinson 's answer, solved by below configuration

@Configuration
public class SessionConfig {

  @Lazy(value = false) // only when spring.main.lazy-initialization=true
  @Bean
  JdbcIndexedSessionRepository jdbcIndexedSessionRepository(
      JdbcOperations jdbcOperations, TransactionOperations transactionOperations) {
    return new JdbcIndexedSessionRepository(jdbcOperations, transactionOperations);
  }
}

Upvotes: 0

Andy Wilkinson
Andy Wilkinson

Reputation: 116231

You can define your own instance as a @Bean in a @Configuration class:

@Bean
JdbcIndexedSessionRepository jdbcIndexedSessionRepository(JdbcOperations jdbcOperations, TransactionOperations transactionOperations) {
    JdbcIndexedSessionRepository repository = new JdbcIndexedSessionRepository(jdbcOperations, transactionOperations);
    // any additional customization that's needed
    return repository;
}

Upvotes: 1

Related Questions