springboot和tomcat jar包冲突

问题

使用外部tomcat启动springboot项目失败,报错:

ERROR: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManager' defined in com.zgglyun.dfdts.config.manager.AppConfig: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
....
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
....
Caused by: org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration

原因

springboot本身提供了内置tomcat,如果引入外部tomcat的话lib文件夹下的jar包会冲突。需要解决冲突。

方法

方法1.移除内置的tomcat

<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>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
 </dependency>   

方法2. 将scop置为provided

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

springboot启动项目和外部tomcat启动有什么区别

springboot内置tomcat 访问项目不需要加项目名 http://localhost:8080/test001

外部的tomcat 访问需要加项目名 http://localhost:8080/myPro/test001

ps:

scop是限制dependendy的作用范围分为五个等级
1)compile:也是默认值适用于所有阶段(编译,运行,测试,发布
2)provided:在编译测试时有效,在运行时无效
3)runtime:在运行,测试时有效,在编译代码时无效
3)test:只在测试时有效
4)system:在编译,测试时有效,在运行时无效。(一般和systemPath一起使用,谨慎使用)

ps ps :

maven常用命令

命令 说明
mvn clean 清理项目产生的临时文件,一般在target下面
mvn compile 编译源代码,一般是src/main/java
mvn package 项目打包生成jar或者war,一般在target下
mvn test 测试命令,一般是执行src/test/java下的junit
mvn install 将打包的文件安装到本地仓库中
mvn depoly 将打包的文件部署到远程仓库
mvn site 生成项目相关网站
mvn temcat:run 用tomcat容器运行项目
mvn jetty:run 用jetty运行项目

maven的生命周期:

1)clean:清理上一次构建生成的文件
2)validate:验证
3)compile:编译
4)test:测试
5)package:打包
6)varify:检查
7)install:安装到本地仓库
8)deploly:部署

原文地址:https://www.cnblogs.com/staystand/p/11951732.html