From 989f24ceafc76be646511b1013ac2f830cc39b46 Mon Sep 17 00:00:00 2001 From: JoseLion Date: Mon, 26 Feb 2024 01:19:21 -0500 Subject: [PATCH] feat(core): Relationships auto-configuration --- .../RelationshipAutoConfiguration.java | 40 +++++++++++++++++++ .../RelationshipCallbacks.java | 7 +--- ...ot.autoconfigure.AutoConfiguration.imports | 1 + 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipAutoConfiguration.java create mode 100644 src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipAutoConfiguration.java b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipAutoConfiguration.java new file mode 100644 index 0000000..0c7faee --- /dev/null +++ b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipAutoConfiguration.java @@ -0,0 +1,40 @@ +package io.github.joselion.springr2dbcrelationships; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; + +import lombok.extern.slf4j.Slf4j; + +/** + * Relationships autoconfiguration. + */ +@Slf4j +@Configuration +public class RelationshipAutoConfiguration { + + /** + * Default configuration constructor. + */ + protected RelationshipAutoConfiguration() { + log.info("R2DBC Relationships auto-configuration loaded."); + } + + /** + * Creates the {@link RelationshipCallbacks} bean. + * + * @param the type of the entity in the callback + * @param template the r2dbc entity template + * @param context the Spring application context + * @return the relationship callbacks bean + */ + @Bean + public RelationshipCallbacks relationshipCallbacks( + final @Lazy R2dbcEntityTemplate template, + final @Lazy ApplicationContext context + ) { + return new RelationshipCallbacks<>(template, context); + } +} diff --git a/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java index de5e9e1..23ed2cb 100644 --- a/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java +++ b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java @@ -10,14 +10,12 @@ import org.reactivestreams.Publisher; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Lazy; import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; import org.springframework.data.r2dbc.mapping.OutboundRow; import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback; import org.springframework.data.r2dbc.mapping.event.AfterSaveCallback; import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback; import org.springframework.data.relational.core.sql.SqlIdentifier; -import org.springframework.stereotype.Component; import io.github.joselion.springr2dbcrelationships.annotations.ManyToMany; import io.github.joselion.springr2dbcrelationships.annotations.ManyToOne; @@ -43,10 +41,9 @@ * @param template the r2dbc entity template * @param context the Spring application context */ -@Component public record RelationshipCallbacks( - @Lazy R2dbcEntityTemplate template, - @Lazy ApplicationContext context + R2dbcEntityTemplate template, + ApplicationContext context ) implements AfterConvertCallback, AfterSaveCallback, BeforeConvertCallback { @Override diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..c6cec55 --- /dev/null +++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +io.github.joselion.springr2dbcrelationships.RelationshipAutoConfiguration