Skip to content

Commit

Permalink
Move spring-data-querydsl-predicate-injector autoconfiguration to tha…
Browse files Browse the repository at this point in the history
…t subproject, to make it more selfcontained
  • Loading branch information
vierbergenlars committed Aug 28, 2023
1 parent 7ac9c7f commit 230a6dc
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 116 deletions.
2 changes: 2 additions & 0 deletions spring-data-querydsl-predicate-injector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.contentgrid.thunx.spring.data.querydsl.predicate.injector.rest.webmvc.SpringDataQuerydslPredicateInjectorAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -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")
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@AutoConfiguration
@ConditionalOnClass(RepositoryRestResource.class)
@AutoConfigureBefore(SpringDataQuerydslPredicateInjectorAutoConfiguration.class)
public class AbacAutoConfiguration {

@EnableAbac
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
com.contentgrid.thunx.api.autoconfigure.SpringDataQuerydslPredicateInjectorAutoConfiguration
com.contentgrid.thunx.api.autoconfigure.AbacAutoConfiguration
com.contentgrid.thunx.gateway.autoconfigure.GatewayAutoConfiguration

0 comments on commit 230a6dc

Please sign in to comment.