Spring boot deployment process analysis (jar or war)

preface

For the deployment of traditional SSM or SSH projects, they are usually packaged into a war package or a compiled folder, and then placed in the webapps directory of Tomcat. If it is a war package, it will be automatically decompressed. By default, a Tomcat is embedded in spring boot, so even web projects can be directly packaged into jar packages and run directly in Java jar.

The web project created with spring initialzr (choose to package it into jar) will only have one spring boot starter web dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

You can find that the dependency includes spring boot starter Tomcat (the dependency package embedded with Tomcat). If you use the internal Tomcat deployment, you do not need to modify the code. Run the main method under xxapplication directly.

If you are creating a war web project, there will be one more servletinitializer file by default, and Maven will have one more spring boot starter Tomcat dependency. The war project can be started with either the main method or the external Tomcat.

It seems that there is no problem. If the previous construction project uses jar and needs to be deployed with external tomcat, only the following modifications need to be made:

// 1. 添加一个 ServletInitializer.java
// 2. Maven 中添加<packaging>war</packaging>。(默认是 jar)
// 3. 添加 spring-boot-starter-tomcat 依赖。(测试过,不加也没关系,但是既然 Spring Initialzr 创建时就自带了,还是加上好了)

If you use JSP, there may be some small problems.

Generally, blogs will write that if JSP is needed, a dependency Tomcat embedded Jasper needs to be added:

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
</dependency>

Special attention should be paid to:

Although spring boot starter web is embedded with tomcat, the embedded spring boot starter Tomcat only contains Tomcat embedded core, not Tomcat embedded Jasper. Therefore, you need to add the dependency separately.

If external Tomcat deployment is used and JSP is used, the following configuration is required:

<!-- 把 web 中内嵌的 tomcat 去掉。(防止和外部 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>

<!-- 注意 tomcat-embed-jasper 的scope。(在编译测试的时候需要这个依赖的参与,但是在部署的时候 tomcat本身就有,这里也是防止冲突) -->
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
</dependency>

summary

reference

springboot embedded tomcat and tomcat-embed-jasper

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>