写了一个spring boot 小demo 结果在本地启动之后不报错,但是也没有启动成功,如下:
一开始怀疑是端口被占用,最后发现不是端口问题。是在项目的pom文件里引入了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
解决一:这个包是可以不用引入的,之间把这个包删除,maven clean 之后运行即可成功。
原因参考:https://www.e-learn.cn/content/wangluowenzhang/1023151 这个链接里说的很清楚,spring-boot-starter-web 包含spring-boot-starter-tomcat 。
spring-boot-starter-web contains spring web dependencies (including spring-boot-starter-tomcat):
spring-boot-starter
jackson
spring-core
spring-mvc
spring-boot-starter-tomcat
spring-boot-starter-tomcat contains everything related to an embdedded tomcat server:
core
el
logging
websocket
What if you want to use spring mvc without the embedded tomcat server?
Just exclude it from the dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
解决二:把provided 改为compile(默认,可不写直接删除scope)。
原因参考:https://www.jianshu.com/p/fb09a49d4d1b
Scope的其他参数如下:
compile:默认的scope,表示 dependency 都可以在生命周期中使用。
而且,这些dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布,作用在编译,测试,运行阶段。
provided:跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性,即:child module无法引用。
runtime:表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段,与provided相反。
test:表示dependency作用在测试时,不作用在运行时。 只在测试时使用,用于编译和运行测试代码。不会随项目发布。
system:跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。
参考:https://www.jianshu.com/p/fb09a49d4d1b
https://www.e-learn.cn/content/wangluowenzhang/1023151