All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.config.SearchableRestConfigure Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.kasinf.framework.rest.config;

import com.kasinf.framework.rest.annotation.Projection;
import com.kasinf.framework.rest.eneity.AbstractEntity;
import com.kasinf.framework.rest.repository.BaseRepository;
import com.kasinf.framework.core.util.MessageUtils;
import com.kasinf.framework.rest.util.ProjectionUtils;
import com.kasinf.framework.rest.util.SearchableConvertUtils;
import com.kasinf.framework.rest.web.controller.BaseController;
import com.kasinf.framework.rest.web.controller.SearchableController;
import com.kasinf.framework.rest.web.controller.SearchableExceptionHandler;
import com.kasinf.framework.rest.web.method.PageableMethodArgumentResolver;
import com.kasinf.framework.rest.web.method.SearchableMethodArgumentResolver;
import com.kasinf.framework.rest.web.service.BaseService;
import com.kasinf.framework.rest.web.util.SearchableBeanUtils;
import com.kasinf.framework.rest.web.util.SpringUtils;
import com.kasinf.framework.rest.web.util.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.util.AnnotatedTypeScanner;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.validation.Validation;
import javax.validation.Validator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lkhsh
 * Searchable配置类
 */
@Configuration
@ComponentScan(
        basePackageClasses = SearchableController.class,
        includeFilters = @Filter(SearchableController.class)
        , useDefaultFilters = false)
@Slf4j
public class SearchableRestConfigure implements WebMvcConfigurer, ApplicationListener {

    @Autowired
    private SearchableConfiguration configuration;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    @ConditionalOnMissingBean
    public MessageUtils messageUtils(MessageSource messageSource) {
        return new MessageUtils(messageSource);
    }

    @Bean
    @ConditionalOnMissingBean
    public SearchableConvertUtils searchableConvertUtils(ConversionService conversionService) {
        return new SearchableConvertUtils(conversionService);
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringUtils springUtils(ConfigurableListableBeanFactory beanFactory) {
        return new SpringUtils(beanFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public ValidatorUtils validatorUtils() {
        Validator validator = Validation.byProvider(HibernateValidator.class)
                .configure()
                .failFast(configuration.getFileFast())
                .buildValidatorFactory()
                .getValidator();
        return new ValidatorUtils(validator);
    }

    @Bean
    @ConditionalOnMissingBean
    public SearchableExceptionHandler searchableExceptionHandler() {
        return new SearchableExceptionHandler();
    }

    @Override
    public void addArgumentResolvers(List resolvers) {
        PageableMethodArgumentResolver pageableMethodArgumentResolver = new PageableMethodArgumentResolver(configuration);
        SearchableMethodArgumentResolver searchableMethodArgumentResolver = new SearchableMethodArgumentResolver(pageableMethodArgumentResolver, configuration);
        resolvers.add(pageableMethodArgumentResolver);
        resolvers.add(searchableMethodArgumentResolver);
    }

    @Bean
    @ConditionalOnMissingBean
    public Repositories repositories() {
        return new Repositories(applicationContext);
    }

    @Bean
    @ConditionalOnMissingBean
    public ProjectionFactory projectionFactory() {
        SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
        projectionFactory.setBeanFactory(applicationContext);
        return projectionFactory;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext context = event.getApplicationContext();
        Map serviceMap = context.getBeansOfType(BaseService.class);
        SearchableBeanUtils.setServiceMap(serviceMap);
        Map repositoryMap = context.getBeansOfType(BaseRepository.class);
        SearchableBeanUtils.setRepositoryMap(repositoryMap);

        Map controllerMap = context.getBeansOfType(BaseController.class);
        controllerMap.forEach((key, value) -> {
            SearchableController searchableController = AnnotationUtils.findAnnotation(value.getClass(), SearchableController.class);
            Class classz = value.getEntityClass();
            if (searchableController != null && classz != null) {
                String mapping = searchableController.value();
                SearchableBeanUtils.addMapping(mapping, classz);
                log.info("Init searchableController={},Mappings={},Entity={}", key, mapping, classz.getSimpleName());
            }
        });


        for (Class projection : getProjections(repositories())) {
            log.info("Padding projection {}", projection);
            ProjectionUtils.addProjection(projection);
        }
    }

    private Set> getProjections(Repositories repositories) {

        Set packagesToScan = new HashSet<>();

        for (Class domainType : repositories) {
            packagesToScan.add(domainType.getPackage().getName());
        }

        AnnotatedTypeScanner scanner = new AnnotatedTypeScanner(Projection.class);
        scanner.setEnvironment(applicationContext.getEnvironment());
        scanner.setResourceLoader(applicationContext);

        return scanner.findTypes(packagesToScan);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy