zoukankan      html  css  js  c++  java
  • SpringBoot配置自定义包扫描

    SpringBoot配置自定义包扫描

    项目中正在拆分微服务,需要把一个maven模块,拆分成2个服务,要根据不同的包的路径,来启动不同的服务,因此就使用了自定义的包扫描,代码如下:

    /**
     * @author WGR
     * @create 2021/8/2 -- 12:26
     */
    @Slf4j
    public class FwTypeFilter implements TypeFilter, EnvironmentAware {
    
        private  String packageScane;
    
        /**
         * 根据配置文件中的package.scan类型,实现动态包扫描
         * 此处不会通过注入的方法引入Environment,因为正在处于扫描阶段,还没有往IOC容器中注入bean
         * @param metadataReader
         * @param metadataReaderFactory
         * @return
         */
        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)  {
            //获取当前正在扫描的类的类信息
            ClassMetadata classMetadata = metadataReader.getClassMetadata();
            String className = classMetadata.getClassName();
            try {
                Class<?> beanClass = ClassUtils.forName(className, FwTypeFilter.class.getClassLoader());
                String packageName = beanClass.getPackage().getName();
                if("scene".equals(packageScane)){
                    if(packageName.contains(".scene.mvc")){
                        return true;
                    }
                }else if("system".equals(packageScane)){
                    if(!packageName.contains(".scene.mvc")){
                        return true;
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return false;
        }
    
        @Override
        public void setEnvironment(Environment environment) {
            packageScane =  environment.getProperty("package.scan");
            log.info("========加载{}服务===========",packageScane);
        }
    }
    

    在启动类上加上自定义的过滤配置

    @ComponentScan(value = {"com.thingjs.framework.feignServer"},
            //此处为自定义的扫描规则
            includeFilters = {@ComponentScan.Filter(type= FilterType.CUSTOM,classes={FwTypeFilter.class}),
                    //下面是公共的依赖
                    @ComponentScan.Filter(type= FilterType.REGEX,pattern="com.thingjs.framework.feignServer.config.*")
            }
            ,useDefaultFilters = false)
    
  • 相关阅读:
    How to alter department in PMS system
    Can't create new folder in windows7
    calculate fraction by oracle
    Long Wei information technology development Limited by Share Ltd interview summary.
    ORACLE BACKUP AND RECOVERY
    DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
    ORACLE_TO_CHAR Function
    电脑BOIS设置
    JSP点击表头排序
    jsp+js实现可排序表格
  • 原文地址:https://www.cnblogs.com/dalianpai/p/15103334.html
Copyright © 2011-2022 走看看