Back to Examples

Spring Boot integration

Seamlessly integrate Litebridge into Spring Boot applications with autoconfiguration, transaction management, and Spring Data repositories.

Starter dependency

Add the litebridge-spring-boot-starter dependency to your project'spom.xml:

Imports
pom.xml
<!-- pom.xml -->
<dependency>
    <groupId>org.litebridge</groupId>
    <artifactId>litebridge-spring-boot-starter</artifactId>
    <version>0.5.0</version>
</dependency>

What does autoconfiguration do?

  • DatabaseProvider detection: Scans the classpath for database providers (e.g. H2, PostgreSQL, Oracle) and selects the available implementation.
  • Transaction management: Configures a LitebridgeTransactionManager backed by the application's DataSource, fully integrated with Spring's @Transactional.
  • Litebridge bean creation: Instantiates the central Litebridge instance and exposes it as an injectable Spring bean.
  • Repository autoconfiguration: Automatically registers any interface extending LitebridgeRepository<T, ID> located in the application's package hierarchy.
  • Entity & DTO scanning: Discovers @Table-annotated entities in packages specified by litebridge.scan-base-package.

Configuration properties

Customize autoconfiguration behavior using YAML or standard Java properties:

Configure Litebridge settings using YAML format in application.yml.

Imports
application.yml
litebridge:
  # Explicit DatabaseProvider class (auto-detected if omitted)
  database-provider:
    class: org.litebridge.db.h2.H2DatabaseProvider
    scan-base-package: org.litebridge.db
  # Package(s) to scan for @Table-annotated entities
  scan-base-package:
    - org.example.entity
    - org.example.dto
  # Strategy when related DTOs are not joined: NULL_IF_NO_JOIN or PARTIAL_OBJECT_IF_NO_JOIN
  related-dto-strategy: PARTIAL_OBJECT_IF_NO_JOIN
  # Enable or disable repository autoconfiguration (default: true)
  repositories:
    enabled: true
PropertyDescriptionDefault
litebridge.database-provider.classFully qualified class name of the DatabaseProvider to use.(Auto-detected)
litebridge.database-provider.scan-base-packageBase package(s) to scan for DatabaseProviderimplementations.org.litebridge.db
litebridge.scan-base-packagePackage(s) to scan for @Table-annotated entities.(None)
litebridge.related-dto-strategyHandling of unjoined related DTOs (NULL_IF_NO_JOIN orPARTIAL_OBJECT_IF_NO_JOIN).NULL_IF_NO_JOIN
litebridge.repositories.enabledControls whether Litebridge Spring Data repositories are automatically registered.true

Entity & mapping registration

Entities and DTO mappings can be discovered automatically via package scanning or explicitly registered using a Spring configuration class:

When litebridge.scan-base-package is configured, Litebridge automatically scans for @Table-annotated classes upon application startup.

Imports
application.yml
# application.yml
litebridge:
  scan-base-package:
    - org.example.entity
    - org.example.mapping

# Entities with @Table annotations and DTO mappings in these packages
# are registered automatically with the Litebridge bean.

Direct Litebridge usage

Inject the Litebridge bean directly into Spring services. Operations seamlessly participate in Spring's @Transactional transaction context:

UserService.java
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.litebridge.orm.Litebridge;

import org.example.entity.User;
import org.example.entity.meta.UserMeta;

import java.util.List;
@Service
public class UserService {

    private final Litebridge litebridge;

    public UserService(Litebridge litebridge) {
        this.litebridge = litebridge;
    }

    @Transactional
    public User createUser(User user) {
        return litebridge.save(user);
    }

    @Transactional(readOnly = true)
    public List<User> findActiveUsers() {
        return litebridge.select(User.class)
            .from(User.class)
            .where(UserMeta.active.isTrue())
            .list();
    }
}

Spring Data repositories

Declare interfaces extending LitebridgeRepository<T, ID> for boilerplate-free CRUD operations, custom derived query methods, native @Query statements, and custom query implementations:

Define a repository by extending LitebridgeRepository<T, ID>. It is automatically detected and registered as a Spring bean.

UserRepository.java
import org.springframework.stereotype.Repository;
import org.litebridge.spring.repository.LitebridgeRepository;

import org.example.entity.User;
@Repository
public interface UserRepository extends LitebridgeRepository<User, Long> {
    // Inherits standard CRUD methods:
    // - findAll(), findById(id), existsById(id), count()
    // - save(entity), saveAll(entities)
    // - delete(entity), deleteById(id), deleteAll()
}