Skip to content

Auto-configuration

spring-ai-autoconfigure-session-jdbc is a Spring Boot auto-configuration that creates a JdbcSessionRepository bean automatically when a single DataSource bean (or a @Primary one) is present. It depends on spring-ai-autoconfigure-session, which creates a DefaultSessionService bean on top of the single (or @Primary) SessionRepository.

The auto-configuration classes live in the org.springaicommunity.session.autoconfigure and org.springaicommunity.session.jdbc.autoconfigure packages. The repository and core API use org.springframework.ai.session.


Dependency

The recommended way to pull in the full auto-configured stack is via the starter (the version comes from the BOM):

<dependency>
    <groupId>org.springaicommunity</groupId>
    <artifactId>spring-ai-starter-session-jdbc</artifactId>
</dependency>

This single dependency gives you:

Bean Created by
JdbcSessionRepository spring-ai-autoconfigure-session-jdbc
DefaultSessionService spring-ai-autoconfigure-session

Schema initialisation

Control when the bundled DDL script runs with:

spring:
  ai:
    session:
      repository:
        jdbc:
          initialize-schema: always    # always | embedded (default) | never
Value Behaviour
embedded Initialise schema only for embedded databases (H2). Default.
always Always run the DDL script on startup. Useful for PostgreSQL/MySQL/MariaDB in dev. The bundled scripts can be re-run safely.
never Never run the DDL script. Manage schema externally (Flyway, Liquibase, etc.).

Configuration properties

Repository: spring.ai.session.repository.jdbc

JdbcSessionRepositoryProperties defines no properties of its own. It extends Spring Boot's DatabaseInitializationProperties and inherits these:

Property Default Description
initialize-schema embedded When to run the bundled DDL script
schema classpath:org/springframework/ai/session/jdbc/schema-@@platform@@.sql DDL script location; @@platform@@ is resolved to the detected database platform
platform (auto-detected) Overrides platform detection used to resolve @@platform@@ in schema
continue-on-error true Whether to continue startup if the DDL script fails (Spring Boot's default for these properties)

Service: spring.ai.session

Property Default Description
time-to-live 60d Default time-to-live of sessions created by the auto-configured DefaultSessionService (any Duration, e.g. 2h, 30d)
allow-system-messages false Whether a SystemMessage may be stored in a session. Disabled by default; see System Messages

Overriding the auto-configured beans

Both auto-configurations use @ConditionalOnMissingBean (for SessionRepository and SessionService respectively), so declare your own bean of either type and the auto-configuration backs off; either or both can be overridden independently. The repository auto-configuration backs off for a user-defined SessionRepository of any implementation type. See Manual bean setup for the builder options (explicit dialect, transaction manager, JSON mapper).


See also