项目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,这样就没问题了,不然你就算让自己的优先加载,你的公共实现就失效了
重新写一个WebMvcConfigurationSupport,里面重写添加拦截器的逻辑,把不需要的remove掉
用WebMvcConfigurer吧,多个可以同时生效