Spring boot and tomcat jar package conflict

problem

Starting springboot project with external tomcat failed with error:

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

Reason

Spring boot itself provides built-in tomcat. If external tomcat is introduced, jar packages in lib folder will conflict. Conflicts need to be resolved.

Method

Method 1. Remove the built-in 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>   

Method 2. Set the scop to 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>

What's the difference between spring boot and external tomcat boot

Spring boot built-in tomcat access project does not need to add project name http://localhost:8080/test001

External tomcat access requires adding project name http://localhost:8080/myPro/test001

ps

scop limits the scope of dependency to five levels 1) compile: it is also the default value applicable to all phases (compile, run, test, publish) 2) provided: valid at compile test time, invalid at run time 3) runtime: valid when running, testing, and compiling code 3) test: valid only when testing 4) system: valid when compiling, testing, and running. (generally used with systemPath, use with caution)

ps ps

maven common commands

command Explain
mvn clean Clean up temporary files generated by the project, usually under target
mvn compile Compile source code, generally src/main/java
mvn package Project packaging generates jar or war, usually under target
mvn test The test command is generally to execute junit under src/test/java
mvn install Install packaged files into the local warehouse
mvn depoly Deploy packaged files to remote warehouse
mvn site Build project related websites
mvn temcat:run Running a project with a tomcat container
mvn jetty:run Running projects with jetty

maven's life cycle:

1) clean: clean up the files generated by the last build 2) validate: verify 3)compile: compile 4)test: Test 5) package: packaging 6) verify: check 7) install: install to local warehouse 8) deploy: deploy

Tags: Programming Tomcat Spring Hibernate Bean Validation

Posted on Thu, 28 Nov 2019 04:54:58 -0500 by Bizzle