Springboot排除不需要的类
发现问题
可以直接翻到结论去看如何排除不需要的类
项目A,包名com.aa
项目B,包名com.bb
我需要在项目A中依赖项目B,但是项目A中配置了JsonConfig,项目B中配置了FastJson2Configuration(未在项目B中显式使用,且引用的依赖中不包含fastjson2),两个类的bean名称冲突了,导致无法启动项目。
解决问题
-
将JsonConfig的方法名变更,启动时,报不能配置两个相同的bean。
-
在ComponentScan中将这个类排除。可以正常启动项目,但是在使用apifox访问项目B中引入的RequestUrl时,报404,找不到这个url。
-
debug spring bean注入的逻辑,发现在使用ComponentScan注解后,scanBasePackages会失效,因此扫描包的写法改为使用ComponentScan中的value
结论
排除自定义bean
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE,
value= {FastJson2Configuration.class})})
当使用ComponentScan去排除自定义bean后,SpringBootApplication注解中的scanBasePackages就不生效了,需要写到ComponentScan的value中。
因此启动类上的写法要改成如下这种:
@ComponentScan(value={"com.aa", "com.bb"},
excludeFilters = {@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE,
value= {FastJson2Configuration.class})})
//@SpringBootApplication(scanBasePackages = {"com.xxxx.xxx"})
@SpringBootApplication
排除自动装配bean
@SpringBootApplication(scanBasePackages = {"com.aa", "com.bb"},
exclude = {WxMaAutoConfiguration.class})