shaozi08 2022-11-06 23:20 采纳率: 100%
浏览 276
已结题

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

  • 写回答

6条回答 默认 最新

  • 游一游走一走 2022-11-07 16:10
    关注
    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);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏
查看更多回答(5条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月7日
  • 已采纳回答 11月7日
  • 赞助了问题酬金19元 11月7日
  • 修改了问题 11月7日
  • 展开全部

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分