Add support for providing default Querydsl bindings.

We now consider default Querydsl bindings when registering a DefaultQuerydslBinderCustomizer bean. The default bindings are applied before applying type-specific bindings.

Closes #206.
Original Pull Request: #2292
This commit is contained in:
Mark Paluch
2021-02-04 10:43:29 +01:00
committed by Christoph Strobl
parent e3a8edf581
commit 4e07b85934
4 changed files with 95 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.querydsl.binding;
import com.querydsl.core.types.EntityPath;
/**
* A component for {@link QuerydslBindings} customization acting as default customizer the given entity path regardless
* of the domain type. Instances can be registered with the application context to be applied.
*
* @author Mark Paluch
* @since 2.5
*/
public interface DefaultQuerydslBinderCustomizer extends QuerydslBinderCustomizer<EntityPath<?>> {}

View File

@@ -15,8 +15,10 @@
*/
package org.springframework.data.querydsl.binding;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
@@ -37,6 +39,7 @@ import com.querydsl.core.types.EntityPath;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
* @since 1.11
*/
public class QuerydslBindingsFactory implements ApplicationContextAware {
@@ -48,6 +51,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
private Optional<AutowireCapableBeanFactory> beanFactory;
private Optional<Repositories> repositories;
private QuerydslBinderCustomizer<EntityPath<?>> defaultCustomizer;
/**
* Creates a new {@link QuerydslBindingsFactory} using the given {@link EntityPathResolver}.
@@ -62,6 +66,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
this.entityPaths = new ConcurrentReferenceHashMap<>();
this.beanFactory = Optional.empty();
this.repositories = Optional.empty();
this.defaultCustomizer = NoOpCustomizer.INSTANCE;
}
/*
@@ -73,6 +78,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
this.beanFactory = Optional.of(applicationContext.getAutowireCapableBeanFactory());
this.repositories = Optional.of(new Repositories(applicationContext));
this.defaultCustomizer = findDefaultCustomizer();
}
/**
@@ -126,6 +132,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
EntityPath<?> path = verifyEntityPathPresent(domainType);
QuerydslBindings bindings = new QuerydslBindings();
defaultCustomizer.customize(bindings, path);
findCustomizerForDomainType(customizer, domainType.getType()).customize(bindings, path);
return bindings;
@@ -151,9 +158,32 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
});
}
/**
* Obtains registered {@link DefaultQuerydslBinderCustomizer} instances from the
* {@link org.springframework.beans.factory.BeanFactory}.
*
* @return
*/
private QuerydslBinderCustomizer<EntityPath<?>> findDefaultCustomizer() {
return beanFactory.map(this::getDefaultQuerydslBinderCustomizer).orElse(NoOpCustomizer.INSTANCE);
}
private QuerydslBinderCustomizer<EntityPath<?>> getDefaultQuerydslBinderCustomizer(
AutowireCapableBeanFactory beanFactory) {
List<DefaultQuerydslBinderCustomizer> customizers = beanFactory
.getBeanProvider(DefaultQuerydslBinderCustomizer.class).stream().collect(Collectors.toList());
return (bindings, root) -> {
for (DefaultQuerydslBinderCustomizer defaultQuerydslBinderCustomizer : customizers) {
defaultQuerydslBinderCustomizer.customize(bindings, root);
}
};
}
/**
* Obtains the {@link QuerydslBinderCustomizer} for the given domain type. Will inspect the given annotation for a
* dedicatedly configured one or consider the domain types's repository.
* dedicated configured one or consider the domain type's repository.
*
* @param annotation
* @param domainType
@@ -194,7 +224,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
}).orElseGet(() -> BeanUtils.instantiateClass(type));
}
private static enum NoOpCustomizer implements QuerydslBinderCustomizer<EntityPath<?>> {
private enum NoOpCustomizer implements QuerydslBinderCustomizer<EntityPath<?>> {
INSTANCE;