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

com.zyy.common.config.ControllerConfig Maven / Gradle / Ivy

package com.zyy.common.config;

import com.zyy.common.annotation.AuthParam;
import com.zyy.common.mapper.MethodMapper;
import com.zyy.common.model.Request;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import java.lang.reflect.Method;
import java.util.*;

@Slf4j
@Configuration
public class ControllerConfig {

    public ControllerConfig(ApplicationContext applicationContext, MethodMapper methodMapper, @Value("${dlg.gateway.path}") String suffix, @Value("${spring.application.name}") String app) {
        long start = System.currentTimeMillis();
        log.info("Init Request Database start .... ");
        // 扫描全部接口
        AbstractHandlerMethodMapping objHandlerMethodMapping = (AbstractHandlerMethodMapping) applicationContext.getBean("requestMappingHandlerMapping");
        Map mapRet = objHandlerMethodMapping.getHandlerMethods();
        List news = new ArrayList<>();
        List old = methodMapper.selectIdByApp(app);
        for (RequestMappingInfo mappingInfo : mapRet.keySet()) {
            Method method = mapRet.get(mappingInfo).getMethod();
            Class clazz = method.getDeclaringClass();
            AuthParam mapping = clazz.getDeclaredAnnotation(AuthParam.class);
            if (mapping == null) continue;
            // 类注解
            Set patterns = mappingInfo.getPatternsCondition().getPatterns();
            for (String pattern : patterns) {
                // 方法注解
                AuthParam methodAuth = method.getDeclaredAnnotation(AuthParam.class);
                // 同步数据库
                news.add(suffix + pattern);
                Request request = new Request(suffix + pattern, methodAuth == null ? "" : methodAuth.methodName(), clazz.getName(), mapping.className(), StringUtils.join(mappingInfo.getMethodsCondition().getMethods(), ","), app, new Date(), 0);
                if (methodMapper.selectByPrimaryKey(request.getRequestId()) == null) {
                    methodMapper.insertSelective(request);
                } else {
                    methodMapper.updateByPrimaryKey(request);
                }
            }
        }
        old.removeAll(news);
        if (old.size() > 0) {
            methodMapper.deleteByIdList(old);
        }
        log.info("Init Request Database end .... {}", System.currentTimeMillis() - start);
        // TODO 通知刷新redis缓存
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy