Reputation: 1263
We have 2 spring-boot(3.3.0) application
web-application
spring.session.jdbc.cleanup-cron
)scheduler-application
spring.main.web-application-type=none
)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
spring-boot-starter-web
to dependencies and works as web-application.spring.main.lazy-initialization=false
because JdbcIndexedSessionRepository#afterPropertiesSet
never called and cron not scheduled.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
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
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