更换豆
实施ApplicationContextAware
这就是我在应用程序上下文中替换bean的方式。
感觉有点。我希望听到一种更好的方法。
@Configurationpublic class CustomQuerydslHandlerMethodArgumentResolverConfig implements ApplicationContextAware { private final RepositoryRestMvcConfiguration repositoryRestMvcConfiguration; @Autowired public CustomQuerydslHandlerMethodArgumentResolverConfig(RepositoryRestMvcConfiguration repositoryRestMvcConfiguration) { this.repositoryRestMvcConfiguration = repositoryRestMvcConfiguration; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ((GenericApplicationContext) applicationContext).getBeanFactory(); beanFactory.destroySingleton(REPO_REQUEST_ARGUMENT_RESOLVER_BEAN_NAME); beanFactory.registerSingleton(REPO_REQUEST_ARGUMENT_RESOLVER_BEAN_NAME, meetupApiRepoRequestArgumentResolver(applicationContext, repositoryRestMvcConfiguration)); } @Bean public RootResourceInformationHandlerMethodArgumentResolver meetupApiRepoRequestArgumentResolver(ApplicationContext applicationContext, RepositoryRestMvcConfiguration repositoryRestMvcConfiguration) { QuerydslBindingsFactory factory = applicationContext.getBean(QuerydslBindingsFactory.class); QuerydslPredicateBuilder predicateBuilder = new QuerydslPredicateBuilder(repositoryRestMvcConfiguration.defaultConversionService(), factory.getEntityPathResolver()); return new CustomQuerydslHandlerMethodArgumentResolver(repositoryRestMvcConfiguration.repositories(), repositoryRestMvcConfiguration.repositoryInvokerFactory(repositoryRestMvcConfiguration.defaultConversionService()), repositoryRestMvcConfiguration.resourcemetadataHandlerMethodArgumentResolver(), predicateBuilder, factory); }}从http参数创建Map-searching谓词
扩展RootResourceInformationHandlerMethodArgumentResolver
这些是基于http查询参数创建我自己的Map-search谓词的代码段。再次-很想知道更好的方法。
该
postProcess方法调用:
predicate = addCustomMapPredicates(parameterMap, predicate, domainType).getValue();
就 在将
predicate引用传递到
QuerydslRepositoryInvokerAdapter构造函数并返回之前。
这是该
addCustomMapPredicates方法:
private BooleanBuilder addCustomMapPredicates(MultiValueMap<String, String> parameters, Predicate predicate, Class<?> domainType) { BooleanBuilder booleanBuilder = new BooleanBuilder(); parameters.keySet() .stream() .filter(s -> s.contains("[") && matches(s) && s.endsWith("]")) .collect(Collectors.toList()) .forEach(paramKey -> {String property = paramKey.substring(0, paramKey.indexOf("["));if (ReflectionUtils.findField(domainType, property) == null) { LOGGER.warn("Skipping predicate matching on [%s]. It is not a known field on domainType %s", property, domainType.getName()); return;}String key = paramKey.substring(paramKey.indexOf("[") + 1, paramKey.indexOf("]"));parameters.get(paramKey).forEach(value -> { if (!StringUtils.hasLength(value)) { booleanBuilder.or(matchesProperty(key, null)); } else { booleanBuilder.or(matchesProperty(key, value)); }}); }); return booleanBuilder.and(predicate); } static boolean matches(String key) { return PATTERN.matcher(key).matches(); }和模式:
private static final Pattern PATTERN = Pattern.compile(".*[^.]\[.*[^\[]");


