The normal boot project is packaged as a jar package, and the boot project has built-in tomcat. The general package is MVN clean install-dmaven. Test. Skip.
Clean remove all files generated by the previous build
Install install the package to the local repository to make other projects dependent
Maven. Test. Skip skip Test
Run the following command: Java-jar XXX. Jar
For some external reasons, we need to package the project into a war package. At this time, we can create a new boot project and select the war package method for reference, this method is officially recommended by spring. In fact, the following operations are performed to pack war files:
1. Add <packaging> war </packaging> in the header of the Pom. xml file ,/
<Groupid> com. uloan. SSM </groupid>
<Artifactid> uloan </artifactid>
<Version> 1.0-Snapshot </version>
<Packaging> war </packaging>
<! -- <Packaging> jar </packaging> -->
2. <! -- Add the following dependency to overwrite the default embedded Tomcat dependency -->
<Dependency>
<Groupid> org. springframework. Boot </groupid>
<Artifactid> spring-boot-starter-Tomcat </artifactid>
<Scope> provided </scope>
</Dependency>
Scope category
1. Compile: the default value indicates that the dependent project needs to participate in the compilation of the current project, as well as subsequent tests. The running cycle is also involved, which is a relatively strong dependency. It is usually included in the package.
2. Test: dependent projects only participate in test-related work, including compilation and execution of test code, and will not be packaged. For example: JUnit
3. runtime: indicates that the dependent project does not need to be involved in project compilation. However, it is required to participate in subsequent testing and running cycles. Compared with compile, compilation is skipped. For example, the JDBC driver is applicable to the running and testing stages.
4. Provided: During packaging, you do not need to package it in. Other facilities will provide it. In fact, this dependency can theoretically participate in the compilation, testing, and running cycles. It is equivalent to compile, but exclude is performed in the packaging stage.
5. System: In terms of engagement, it is the same as provided, but the dependencies are not downloaded from the maven repository, but obtained from the local file system. You need to add the systempath attribute to define the path.
Therefore, we use provided here, which is also officially recommended by spring.
The rest is to modify our startup class. We previously started the class as the boot method, and we want to change it to the Tomcat method, of course, the previous boot methods are still used locally.
Public class servletinitializer extends springbootservletinitializer {
@ Override
Protected springapplicationbuilder configure (springapplicationbuilder application ){
Return application. Sources (uloanapplication. Class );
}
}
This class inherits springbootservletinitializer and overwrites the configure method.
Differences between jar package and war package startup
Jar package: Execute the run method of springbootapplication, start the IOC container, and then create an embedded Servlet Container
War package: first start the servlet server, the server starts the springboot application (springbootservletinitizer), and then starts the IOC container
When the springbootservletinitializer instance executes the onstartup method, it will execute the run method through the createrootapplicationcontext method. The next process is the same as the run process of the application started in the form of a jar package, an IOC container is created and returned internally. However, when an application in the form of a war Package creates an IOC container, the servlet container is no longer created.
Create a war package for the stringboot Project