diff --git a/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipsAutoConfiguration.java b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipsAutoConfiguration.java new file mode 100644 index 0000000..22e66ce --- /dev/null +++ b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipsAutoConfiguration.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 RelationshipsAutoConfiguration { + + /** + * Default configuration constructor. + */ + protected RelationshipsAutoConfiguration() { + log.info("R2DBC Relationships auto-configuration loaded."); + } + + /** + * Creates the {@link RelationshipsCallbacks} 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 RelationshipsCallbacks relationshipsCallbacks( + final @Lazy R2dbcEntityTemplate template, + final @Lazy ApplicationContext context + ) { + return new RelationshipsCallbacks<>(template, context); + } +} diff --git a/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipsCallbacks.java similarity index 95% rename from src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java rename to src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipsCallbacks.java index de5e9e1..6822dd1 100644 --- a/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacks.java +++ b/src/main/java/io/github/joselion/springr2dbcrelationships/RelationshipsCallbacks.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 +public record RelationshipsCallbacks( + R2dbcEntityTemplate template, + ApplicationContext context ) implements AfterConvertCallback, AfterSaveCallback, BeforeConvertCallback { @Override @@ -163,19 +160,19 @@ public Publisher onBeforeConvert(final T entity, final SqlIdentifier table) { private UnaryOperator addToContextStack(final T entity) { return ctx -> { final var typeName = entity.getClass().getName(); - final var stack = ctx.>getOrEmpty(RelationshipCallbacks.class) + final var stack = ctx.>getOrEmpty(RelationshipsCallbacks.class) .map(List::stream) .map(prev -> Stream.concat(prev, Stream.of(typeName))) .map(Stream::toList) .orElse(List.of(typeName)); - return ctx.put(RelationshipCallbacks.class, stack); + return ctx.put(RelationshipsCallbacks.class, stack); }; } private Mono checkingCycles(final S data) { return Mono.deferContextual(ctx -> { - final var stack = ctx.>getOrEmpty(RelationshipCallbacks.class); + final var stack = ctx.>getOrEmpty(RelationshipsCallbacks.class); return Mono.justOrEmpty(stack) .defaultIfEmpty(List.of()) diff --git a/src/main/java/io/github/joselion/springr2dbcrelationships/processors/ManyToManyProcessor.java b/src/main/java/io/github/joselion/springr2dbcrelationships/processors/ManyToManyProcessor.java index a106864..0574357 100644 --- a/src/main/java/io/github/joselion/springr2dbcrelationships/processors/ManyToManyProcessor.java +++ b/src/main/java/io/github/joselion/springr2dbcrelationships/processors/ManyToManyProcessor.java @@ -21,7 +21,7 @@ import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback; import org.springframework.data.relational.core.sql.SqlIdentifier; -import io.github.joselion.springr2dbcrelationships.RelationshipCallbacks; +import io.github.joselion.springr2dbcrelationships.RelationshipsCallbacks; import io.github.joselion.springr2dbcrelationships.annotations.ManyToMany; import io.github.joselion.springr2dbcrelationships.annotations.OneToMany; import io.github.joselion.springr2dbcrelationships.exceptions.RelationshipException; @@ -237,7 +237,7 @@ private Mono findJoinTable(final Field field) { private Mono checkingBackRef(final Class type, final S value) { return Mono.deferContextual(ctx -> { - final var stack = ctx.>getOrEmpty(RelationshipCallbacks.class); + final var stack = ctx.>getOrEmpty(RelationshipsCallbacks.class); return Mono.justOrEmpty(stack) .defaultIfEmpty(List.of()) 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 diff --git a/src/test/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacksTest.java b/src/test/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacksTest.java index 4b14026..abaa505 100644 --- a/src/test/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacksTest.java +++ b/src/test/java/io/github/joselion/springr2dbcrelationships/RelationshipCallbacksTest.java @@ -25,13 +25,13 @@ final var table = SqlIdentifier.unquoted("test_entity"); final var template = mock(R2dbcEntityTemplate.class); final var context = mock(ApplicationContext.class); - final var callbacks = new RelationshipCallbacks<>(template, context); + final var callbacks = new RelationshipsCallbacks<>(template, context); final var publisher = callbacks.onAfterConvert(entity, table); Mono.from(publisher) .as(StepVerifier::create) .expectAccessibleContext() - .contains(RelationshipCallbacks.class, List.of(TestEntity.class.getName())) + .contains(RelationshipsCallbacks.class, List.of(TestEntity.class.getName())) .then() .expectNext(entity) .verifyComplete();