SpringBoot项目多个类继承WebMvcConfigurationSupport只让指定的一个生效

项目demo:demo
demo项目就是公共的项目,也是启动类,里面的东西啥都不能修改,只能修改properties配置文件。zijixiangmu就是能改的代码,目前我项目里面的MVC配置就没生效。
目前的问题:
@ComponentScan(basePackages={"com.gongsi"})改为下面的样子
@ComponentScan(basePackages={"com.gongsi"}, excludeFilters =@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {A.class}))
但是只能是配置文件,是properties,不是XML
或者是解答下面的问题:
SpringBoot同时继承多个WebMvcConfigurationSupport只让指定的一个生效
自己的项目代码会打成Jar包放到公司公共的一个SpringBoot项目中去启动,现在需要添加几个拦截器,但是公共的项目是一个类继承了WebMvcConfigurationSupport,导致自己添加配置类根本不会起作用,并且公司的这个类和启动类是在同一级目录的。
目前没有权利去修改这个公共的项目,只能改自己项目代码或改配置文件。
现在就是想让这个公共项目里的配置类失效,或者是自己的配置类能够早于这个被注入。
公共项目的配置类

@Component
public class A extends WebMvcConfigurationSupport{
//省略了实现
}

自己看的一些文章:
https://blog.csdn.net/weixin_45285213/article/details/123904944
https://www.cnblogs.com/guoxiaoyu/p/13731123.html
https://blog.csdn.net/zpx_Smart/article/details/124559942
https://blog.csdn.net/Junna_zeng/article/details/123537881

package com.example.demo.test;

import com.example.demo.test.ZiJiInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @date 2022/11/7
 */
@Component
public class RequestMappingConfiguration {
    @Autowired
    List<AbstractHandlerMapping> abstractHandlerMappings;

    @PostConstruct
    private void init() throws Exception {
        InterceptorRegistry registry = new InterceptorRegistry();
        registry.addInterceptor(new ZiJiInterceptor()).addPathPatterns("/demo/**");
        Method getInterceptors = ReflectionUtils.findMethod(InterceptorRegistry.class, "getInterceptors");
        ReflectionUtils.makeAccessible(getInterceptors);
        final List<HandlerInterceptor> invoke = (List<HandlerInterceptor>) getInterceptors.invoke(registry);
        Field adaptedInterceptors = ReflectionUtils.findField(AbstractHandlerMapping.class, "adaptedInterceptors");
        ReflectionUtils.makeAccessible(adaptedInterceptors);
        for (AbstractHandlerMapping item:abstractHandlerMappings ) {
            ((List<HandlerInterceptor>) adaptedInterceptors.get(item)).addAll(invoke);
        }
    }
}

给我个最小项目demo,保证完成任务

WebMvcConfigurer+1,公共依赖别去继承WebMvcConfigurationSupport,而是去继承DelegatingWebMvcConfiguration,自己业务方实现WebMvcConfigurer,这样就没问题了,不然你就算让自己的优先加载,你的公共实现就失效了

参考链接

SpringBoot使用@Configuration、@Bean、@ComponentScan注解实现Java配置方式_pan_junbiao的博客-CSDN博客 1、XML配置方式在Java项目开发中,使用Spring框架,如果使用传统的XML配置方式,需要首先创建Spring的核心配置文件(applicationContext.xml),然后配置Bean等相关信息。配置文件如下:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframe... https://blog.csdn.net/pan_junbiao/article/details/104594766?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166778412316800182133091%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=166778412316800182133091&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-104594766-null-null.nonecase&utm_term=SpringBoot%20%40ComponentScan%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AE%9E%E7%8E%B0%EF%BC%88properties%E5%AE%9E%E7%8E%B0%EF%BC%89&spm=1018.2226.3001.4450

重新写一个WebMvcConfigurationSupport,里面重写添加拦截器的逻辑,把不需要的remove掉

用WebMvcConfigurer吧,多个可以同时生效