需求
因为springboot项目打包默认会把第三方jar也打包进项目。这样就导致我们的项目有100M,然而我们自己的项目一共100K。
遇到的bug
-
这个报错很奇怪,我直接在idea右键启动不会报错,但是打成jar在命令行就报错。
-
因为项目是多模块开发的,我开始打算把各个模块所有的第三方jar统一放到一个目录下,然后使用java -Dloader.path引入:
java -Dloader.path=/home/ssx/appdata/demo -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
-
启动项目后就一直spring报错创建Bean失败:
Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available:
问题解决
- 根据报错信息查看对应的bean信息,发现是
org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class
这个对象创建失败,发现这个bean竟然是gateway
的依赖,但是我启动的项目是eureka啊?? 很疑惑
- 排查原因应该是
-Dloader.path=第三方jar路径
的问题,因为我把整个项目的第三方jar都放到一个目录下了,导致springboot的starter机制:把gateway也给实例化了
- 最终的解决方案:
方法1:把每个模块的jar放到不同的路径下:
mvn -q dependency:copy-dependencies -DoutputDirectory=各个模块路径 -DincludeScope=runtime
方法2:配置springboot的jar扫描(不推荐,因为要手动排除所有的不是本项目的jar)
@SpringBootApplication(excludeName = "**.gateway.**") public class EurekaServerApplication {