From 230a6dcdc6df178663955bead52ebd43fb012df1 Mon Sep 17 00:00:00 2001 From: Lars Vierbergen Date: Mon, 28 Aug 2023 09:54:39 +0200 Subject: [PATCH] Move spring-data-querydsl-predicate-injector autoconfiguration to that subproject, to make it more selfcontained --- .../build.gradle | 2 + ...dslPredicateInjectorAutoConfiguration.java | 77 +++++++++++++++++++ ...pringDataRestIntegrationConfiguration.java | 75 ------------------ ...ot.autoconfigure.AutoConfiguration.imports | 1 + ...edicateInjectorAutoConfigurationTest.java} | 7 +- .../autoconfigure/AbacAutoConfiguration.java | 2 + ...dslPredicateInjectorAutoConfiguration.java | 37 --------- ...ot.autoconfigure.AutoConfiguration.imports | 1 - 8 files changed, 86 insertions(+), 116 deletions(-) create mode 100644 spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfiguration.java delete mode 100644 spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfiguration.java create mode 100644 spring-data-querydsl-predicate-injector/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/{SpringDataRestIntegrationConfigurationTest.java => SpringDataQuerydslPredicateInjectorAutoConfigurationTest.java} (95%) delete mode 100644 thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/SpringDataQuerydslPredicateInjectorAutoConfiguration.java diff --git a/spring-data-querydsl-predicate-injector/build.gradle b/spring-data-querydsl-predicate-injector/build.gradle index 14fc02f3..4350b31f 100644 --- a/spring-data-querydsl-predicate-injector/build.gradle +++ b/spring-data-querydsl-predicate-injector/build.gradle @@ -14,6 +14,8 @@ dependencies { implementation 'org.springframework.data:spring-data-rest-webmvc' + compileOnly "org.springframework.boot:spring-boot-autoconfigure" // for autoconfiguration annotations + testCompileOnly 'org.projectlombok:lombok' testAnnotationProcessor 'org.projectlombok:lombok' diff --git a/spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfiguration.java b/spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfiguration.java new file mode 100644 index 00000000..d2bbf736 --- /dev/null +++ b/spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfiguration.java @@ -0,0 +1,77 @@ +package com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc; + +import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.repository.RepositoryInvokerAdapterFactory; +import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.resolver.QuerydslPredicateResolver; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.ConversionService; +import org.springframework.data.querydsl.binding.QuerydslBindingsFactory; +import org.springframework.data.repository.support.Repositories; +import org.springframework.data.repository.support.RepositoryInvokerFactory; +import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; +import org.springframework.data.rest.webmvc.config.ResourceMetadataHandlerMethodArgumentResolver; +import org.springframework.data.rest.webmvc.config.RootResourceInformationHandlerMethodArgumentResolver; + +@AutoConfiguration +@AutoConfigureAfter(RepositoryRestMvcAutoConfiguration.class) +@ConditionalOnClass(RepositoryRestMvcConfiguration.class) +@ConditionalOnBean(RepositoryRestMvcConfiguration.class) +public class SpringDataQuerydslPredicateInjectorAutoConfiguration { + + @Bean + BeanPostProcessor interceptRepositoryRestMvcConfiguration(ApplicationContext applicationContext) { + return new BeanPostProcessor() { + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + + if (bean instanceof RootResourceInformationHandlerMethodArgumentResolver) { + var repositories = applicationContext.getBean(Repositories.class); + var invokerFactory = applicationContext.getBean(RepositoryInvokerFactory.class); + var resourceMetadataResolver = applicationContext.getBean( + ResourceMetadataHandlerMethodArgumentResolver.class); + + var predicateResolvers = applicationContext.getBeanProvider(QuerydslPredicateResolver.class); + var repositoryInvokerAdapterFactory = applicationContext.getBean( + RepositoryInvokerAdapterFactory.class); + + return new PredicateInjectingRootResourceInformationHandlerMethodArgumentResolver( + repositories, + invokerFactory, + resourceMetadataResolver, + repositoryInvokerAdapterFactory, + predicateResolvers + ); + } + + return bean; + } + }; + } + + @Bean + @ConditionalOnBean(QuerydslBindingsPredicateResolver.class) + @ConditionalOnMissingBean + RepositoryInvokerAdapterFactory repositoryInvokerAdapterFactory(Repositories repositories) { + return new QuerydslRepositoryInvokerAdapterFactory(repositories); + } + + @Bean + @ConditionalOnBean(QuerydslBindingsFactory.class) + QuerydslPredicateResolver querydslBindingsPredicateResolver( + @Qualifier("defaultConversionService") ConversionService conversionService, + QuerydslBindingsFactory querydslBindingsFactory + ) { + return new QuerydslBindingsPredicateResolver(conversionService, querydslBindingsFactory); + } + +} diff --git a/spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfiguration.java b/spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfiguration.java deleted file mode 100644 index 8dc4b170..00000000 --- a/spring-data-querydsl-predicate-injector/src/main/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfiguration.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc; - -import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.repository.RepositoryInvokerAdapterFactory; -import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.resolver.QuerydslPredicateResolver; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.convert.ConversionService; -import org.springframework.data.querydsl.binding.QuerydslBindingsFactory; -import org.springframework.data.repository.support.Repositories; -import org.springframework.data.repository.support.RepositoryInvokerFactory; -import org.springframework.data.rest.webmvc.config.ResourceMetadataHandlerMethodArgumentResolver; -import org.springframework.data.rest.webmvc.config.RootResourceInformationHandlerMethodArgumentResolver; - -@Configuration(proxyBeanMethods = false) -public class SpringDataRestIntegrationConfiguration { - - @Configuration(proxyBeanMethods = false) - public static class RootResourceInformationHandlerMethodArgumentResolverConfiguration { - - @Bean - BeanPostProcessor interceptRepositoryRestMvcConfiguration(ApplicationContext applicationContext) { - return new BeanPostProcessor() { - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - - if (bean instanceof RootResourceInformationHandlerMethodArgumentResolver) { - var repositories = applicationContext.getBean(Repositories.class); - var invokerFactory = applicationContext.getBean(RepositoryInvokerFactory.class); - var resourceMetadataResolver = applicationContext.getBean( - ResourceMetadataHandlerMethodArgumentResolver.class); - - var predicateResolvers = applicationContext.getBeanProvider(QuerydslPredicateResolver.class); - var repositoryInvokerAdapterFactory = applicationContext.getBean( - RepositoryInvokerAdapterFactory.class); - - return new PredicateInjectingRootResourceInformationHandlerMethodArgumentResolver( - repositories, - invokerFactory, - resourceMetadataResolver, - repositoryInvokerAdapterFactory, - predicateResolvers - ); - } - - return bean; - } - }; - } - } - - @Configuration(proxyBeanMethods = false) - public static class DefaultQuerydslBindingsPredicateResolverConfiguration { - - @Bean - QuerydslPredicateResolver querydslBindingsPredicateResolver( - @Qualifier("defaultConversionService") ConversionService conversionService, - QuerydslBindingsFactory querydslBindingsFactory - ) { - return new QuerydslBindingsPredicateResolver(conversionService, querydslBindingsFactory); - } - } - - @Configuration(proxyBeanMethods = false) - public static class DefaultQuerydslRepositoryInvokerAdapterFactoryConfiguration { - - @Bean - RepositoryInvokerAdapterFactory repositoryInvokerAdapterFactory(Repositories repositories) { - return new QuerydslRepositoryInvokerAdapterFactory(repositories); - } - } -} diff --git a/spring-data-querydsl-predicate-injector/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-data-querydsl-predicate-injector/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..aa65f2c1 --- /dev/null +++ b/spring-data-querydsl-predicate-injector/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc.SpringDataQuerydslPredicateInjectorAutoConfiguration \ No newline at end of file diff --git a/spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfigurationTest.java b/spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfigurationTest.java similarity index 95% rename from spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfigurationTest.java rename to spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfigurationTest.java index 169b56cb..e590c741 100644 --- a/spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataRestIntegrationConfigurationTest.java +++ b/spring-data-querydsl-predicate-injector/src/test/java/com/contentgrid/thunx/spring/data/querydsl/predicate/injector/rest/webmvc/SpringDataQuerydslPredicateInjectorAutoConfigurationTest.java @@ -47,20 +47,21 @@ @AutoConfigureMockMvc @Transactional @Testcontainers -class SpringDataRestIntegrationConfigurationTest { +class SpringDataQuerydslPredicateInjectorAutoConfigurationTest { @Configuration @EnableAutoConfiguration @EntityScan() @EnableJpaRepositories(considerNestedRepositories = true) - @Import(SpringDataRestIntegrationConfiguration.class) + @Import(SpringDataQuerydslPredicateInjectorAutoConfiguration.class) static class TestConfiguration { + @Bean QuerydslPredicateResolver additionalPredicate() { return (methodParameter, domainType, parameters) -> { if(parameters.containsKey("test") && domainType == TestEntity.class) { return Optional.of(new CollectionFilteringOnlyOperationPredicates( - QSpringDataRestIntegrationConfigurationTest_TestEntity.testEntity + QSpringDataQuerydslPredicateInjectorAutoConfigurationTest_TestEntity.testEntity .value .eq("entity2") )); diff --git a/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/AbacAutoConfiguration.java b/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/AbacAutoConfiguration.java index cf803120..69bedd20 100644 --- a/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/AbacAutoConfiguration.java +++ b/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/AbacAutoConfiguration.java @@ -1,6 +1,7 @@ package com.contentgrid.thunx.api.autoconfigure; import com.contentgrid.thunx.spring.data.EnableAbac; +import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc.SpringDataQuerydslPredicateInjectorAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -8,6 +9,7 @@ @AutoConfiguration @ConditionalOnClass(RepositoryRestResource.class) +@AutoConfigureBefore(SpringDataQuerydslPredicateInjectorAutoConfiguration.class) public class AbacAutoConfiguration { @EnableAbac diff --git a/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/SpringDataQuerydslPredicateInjectorAutoConfiguration.java b/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/SpringDataQuerydslPredicateInjectorAutoConfiguration.java deleted file mode 100644 index 5fb5d5f2..00000000 --- a/thunx-autoconfigure/src/main/java/com/contentgrid/thunx/api/autoconfigure/SpringDataQuerydslPredicateInjectorAutoConfiguration.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.contentgrid.thunx.api.autoconfigure; - -import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc.QuerydslBindingsPredicateResolver; -import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.repository.RepositoryInvokerAdapterFactory; -import com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc.SpringDataRestIntegrationConfiguration; -import org.springframework.boot.autoconfigure.AutoConfiguration; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; -import org.springframework.context.annotation.Import; -import org.springframework.data.querydsl.binding.QuerydslBindingsFactory; -import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; - -@AutoConfiguration -@AutoConfigureAfter({ - RepositoryRestMvcAutoConfiguration.class, - AbacAutoConfiguration.class -}) -@ConditionalOnClass(RepositoryRestMvcConfiguration.class) -@ConditionalOnBean(RepositoryRestMvcConfiguration.class) -@Import(SpringDataRestIntegrationConfiguration.RootResourceInformationHandlerMethodArgumentResolverConfiguration.class) -public class SpringDataQuerydslPredicateInjectorAutoConfiguration { - - @ConditionalOnBean(QuerydslBindingsFactory.class) - @Import(SpringDataRestIntegrationConfiguration.DefaultQuerydslBindingsPredicateResolverConfiguration.class) - public static class DefaultQuerydslBindingsPredicateResolver { - - @ConditionalOnBean(QuerydslBindingsPredicateResolver.class) - @ConditionalOnMissingBean(RepositoryInvokerAdapterFactory.class) - @Import(SpringDataRestIntegrationConfiguration.DefaultQuerydslRepositoryInvokerAdapterFactoryConfiguration.class) - public static class DefaultQuerydslRepositoryInvokerAdapterFactory { - - } - } -} diff --git a/thunx-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/thunx-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index adcb44f1..37b7a8d8 100644 --- a/thunx-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/thunx-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,2 @@ -com.contentgrid.thunx.api.autoconfigure.SpringDataQuerydslPredicateInjectorAutoConfiguration com.contentgrid.thunx.api.autoconfigure.AbacAutoConfiguration com.contentgrid.thunx.gateway.autoconfigure.GatewayAutoConfiguration \ No newline at end of file