# Spring Boot Starter: Auto-Configuration for Morphium
`morphium-spring-boot-*` is an **optional Morphium module** that integrates Morphium
into [Spring Boot](https://spring.io/projects/spring-boot) applications via
auto-configuration, type-safe `@ConfigurationProperties`, declarative transactions,
an Actuator health indicator, and Jakarta Data `@Repository` interfaces backed by JDK
dynamic proxies at runtime — no build-time bytecode generation, no annotation
processor for the repositories themselves. It pulls in
[`morphium-jakarta-data`](jakarta-data.md) for the entire query-derivation, JDQL, and
pagination runtime.
!!! note "Optional module — the Morphium core does not depend on it"
`de.caluga:morphium` has zero compile- or runtime dependency on this module, on
Spring, or on `jakarta.data-api`. You only need `morphium-spring-boot-starter` if
you are building a Spring Boot application against MongoDB via Morphium.
## What it provides
- **Auto-configuration** — `MorphiumAutoConfiguration` creates the application's
single `Morphium` bean from `morphium.*` properties, with connection retry on
transient failures (linear backoff).
- **Type-safe configuration** — every setting lives under `morphium.*` as
`@ConfigurationProperties`, with `spring-boot-configuration-processor`-generated
metadata for IDE autocompletion.
- **Jakarta Data repositories** — declare a `@Repository` interface extending
`CrudRepository`/`MorphiumRepository` from `morphium-jakarta-data`; at Spring
context-startup time, `MorphiumRepositoryRegistrar` scans for such interfaces and
registers a `MorphiumRepositoryFactoryBean` for each, which creates a
`java.lang.reflect.Proxy` implementing the interface — see
[Proxy mechanism vs. Quarkus](#proxy-mechanism-vs-quarkus) below. See
[Jakarta Data](jakarta-data.md) for the full query-derivation, JDQL, and pagination
feature set — everything documented there works identically once wired through this
module's proxies.
- **Declarative transactions** — `@MorphiumTransactional` on a Spring bean method
wraps the method body in `startTransaction()`/`commitTransaction()`/
`abortTransaction()` via an AspectJ `@Around` advice, active only when
`spring-boot-starter-aop` is on the classpath.
- **Actuator health** — a `HealthIndicator` reporting live MongoDB connection status
(database, driver, replica-set state) under `/actuator/health`, active only when
`spring-boot-actuator` is present and a `Morphium` bean already exists.
- **Test support** — the companion `morphium-spring-boot-test` module provides
`@MorphiumTest`, a composite annotation that wires `InMemDriver` (Morphium's
in-memory MongoDB emulation) into a `@SpringBootTest`, so repository tests run
without a MongoDB instance or container.
## Installation
```xml
de.calugamorphium-spring-boot-starter6.3.9
```
This module follows Morphium's regular release versioning — it is versioned and
released in lockstep with Morphium core; there is no separate version line to track.
## Configuration Reference
All properties live under `morphium.*` (not `spring.morphium.*` — the `spring.*`
namespace is reserved for Spring Boot's own configuration keys). Every entry below is
verified directly against `MorphiumProperties.java` in the
`morphium-spring-boot-autoconfigure` module.
| Property | Default | Description | Source |
|---|---|---|---|
| `morphium.database` | *(required)* | MongoDB database name | `MorphiumProperties.java:46` |
| `morphium.hosts` | `localhost:27017` | Comma-separated `host:port` list; ignored if `morphium.atlas-url` is set | `MorphiumProperties.java:39` |
| `morphium.username` / `.password` | -- | Optional credentials, applied only when both are set | `MorphiumProperties.java:52,57` |
| `morphium.auth-database` | `admin` | Authentication database (`authSource`) | `MorphiumProperties.java:64` |
| `morphium.driver-name` | `PooledDriver` | `PooledDriver` (production) or `InMemDriver` (tests, no MongoDB needed) | `MorphiumProperties.java:71` |
| `morphium.read-preference` | `primary` | MongoDB read preference | `MorphiumProperties.java:77` |
| `morphium.max-connections` | `250` | Connection pool size | `MorphiumProperties.java:82` |
| `morphium.atlas-url` | -- | MongoDB Atlas SRV connection string (overrides `morphium.hosts` when set) | `MorphiumProperties.java:89` |
| `morphium.replica-set-name` | -- | Replica set name (required for transactions) | `MorphiumProperties.java:97` |
| `morphium.connect-retries` | `5` | Connection attempts before giving up on transient failures, linear backoff `attempt * 2000`ms | `MorphiumProperties.java:106` |
| `morphium.index-check` | `CREATE_ON_STARTUP` | `CREATE_ON_STARTUP`, `WARN_ON_STARTUP`, `CREATE_ON_WRITE_NEW_COL`, `NO_CHECK` | `MorphiumProperties.java:115` |
| `morphium.cache.global-valid-time` | `5000` | Cache TTL in milliseconds | `MorphiumProperties.java:361` |
| `morphium.cache.read-cache-enabled` | `true` | Enable query result cache | `MorphiumProperties.java:368` |
| `morphium.ssl.enabled` | `false` | Enable TLS | `MorphiumProperties.java:418` |
| `morphium.ssl.keystore-path` / `.keystore-password` | -- | Keystore (JKS/PKCS12) for client-certificate TLS | `MorphiumProperties.java:426,431` |
If `spring-boot-configuration-processor` is on the classpath (declared as an optional
dependency of `morphium-spring-boot-autoconfigure`), every property above also appears
in `META-INF/spring-configuration-metadata.json`, giving IDEs autocompletion and
validation for `morphium.*` keys.
## Quick Example
```java
@Entity(collectionName = "products")
public class Product {
@Id private MorphiumId id;
private String name;
private double price;
private String category;
// getters/setters omitted
}
@Repository
public interface ProductRepository extends MorphiumRepository {
List findByCategory(String category);
List findByPriceGreaterThan(double minPrice);
}
@SpringBootApplication
@EnableMorphiumRepositories
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Service
public class ProductService {
@Autowired ProductRepository products;
public List findExpensive(double minPrice) {
return products.findByPriceGreaterThan(minPrice);
}
}
```
```properties
morphium.database=my-app-db
morphium.hosts=localhost:27017
```
## Repository Usage
Annotate a `@SpringBootApplication` (or any `@Configuration` class) with
`@EnableMorphiumRepositories` to enable scanning. By default the scan covers the
annotated class's package and sub-packages; pass explicit packages via `value()`/
`basePackages()` to scan elsewhere.
Repository interfaces extend either `jakarta.data.repository.CrudRepository`
(the plain Jakarta Data interface) or `de.caluga.morphium.data.MorphiumRepository`, which adds Morphium-specific escape hatches with no Jakarta Data equivalent:
```java
// Distinct values for a field
List