DATACMNS-1235 - Polishing.

Introduced BeanLookup to easily create a Lazy<T>-based lookup of a unique bean by type on a ListableBeanFactory. This is in support of making it easy for downstream Spring Data modules to consume the EntityPathResolver declared in an ApplicationContext using XML configuration.

QuerydslWebConfiguration now uses a plain ObjectProvider to access the bean defined falling back to our default.

Original pull request: #265.
This commit is contained in:
Oliver Gierke
2018-01-10 12:02:28 +01:00
parent 408b9abd88
commit dcb8166bbc
4 changed files with 189 additions and 1 deletions

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2018 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
*
* http://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.util;
import lombok.experimental.UtilityClass;
import java.util.Map;
import javax.annotation.Nullable;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.util.Assert;
/**
* Simple helper to allow lenient lookup of beans of a given type from a {@link ListableBeanFactory}. This is not user
* facing API but a mere helper for Spring Data configuration code.
*
* @author Oliver Gierke
* @since 2.1
* @soundtrack Dave Matthews Band - Bartender (DMB Live 25)
*/
@UtilityClass
public class BeanLookup {
/**
* Returns a {@link Lazy} for the unique bean of the given type from the given {@link BeanFactory} (which needs to be
* a {@link ListableBeanFactory}). The lookup will produce a {@link NoUniqueBeanDefinitionException} in case multiple
* beans of the given type are available in the given {@link BeanFactory}.
*
* @param type must not be {@literal null}.
* @param beanFactory the {@link BeanFactory} to lookup the bean from.
* @return a {@link Lazy} for the unique bean of the given type or the instance provided by the fallback in case no
* bean of the given type can be found.
*/
public static <T> Lazy<T> lazyIfAvailable(Class<T> type, BeanFactory beanFactory) {
Assert.notNull(type, "Type must not be null!");
Assert.isInstanceOf(ListableBeanFactory.class, beanFactory);
return Lazy.of(() -> lookupBean(type, (ListableBeanFactory) beanFactory));
}
/**
* Looks up the unique bean of the given type from the given {@link ListableBeanFactory}.
*
* @param type must not be {@literal null}.
* @param beanFactory must not be {@literal null}.
* @return
*/
@Nullable
private static <T> T lookupBean(Class<T> type, ListableBeanFactory beanFactory) {
Map<String, T> names = beanFactory.getBeansOfType(type, false, false);
switch (names.size()) {
case 0:
return null;
case 1:
return names.values().iterator().next();
default:
throw new NoUniqueBeanDefinitionException(type, names.keySet());
}
}
}

View File

@@ -19,12 +19,14 @@ import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
import org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver;
@@ -45,6 +47,7 @@ import com.querydsl.core.types.Predicate;
public class QuerydslWebConfiguration implements WebMvcConfigurer {
@Autowired @Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService;
@Autowired ObjectProvider<EntityPathResolver> resolver;
/**
* Default {@link QuerydslPredicateArgumentResolver} to create Querydsl {@link Predicate} instances for Spring MVC
@@ -61,7 +64,7 @@ public class QuerydslWebConfiguration implements WebMvcConfigurer {
@Lazy
@Bean
public QuerydslBindingsFactory querydslBindingsFactory() {
return new QuerydslBindingsFactory(SimpleEntityPathResolver.INSTANCE);
return new QuerydslBindingsFactory(resolver.getIfUnique(() -> SimpleEntityPathResolver.INSTANCE));
}
/*