Interviewer: I want to run the Spring Boot project in tomcat, how should I configure it?

Insert picture description here

Introduction

  1. Inherit SpringBootServletInitializer and override the configure method
  2. The scope of spring-boot-starter-tomcat is changed to provided
  3. Change the packaging method to war
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>
<packaging>war</packaging>

This question is generally asked to see if you are familiar with scope and its role. Just answer the scope

Let's briefly introduce the relevant knowledge of maven

The days before Maven

A small personal feeling, learning a new technology, should look at the reasons for the emergence of this new technology from a historical perspective and what problems it helped us solve. Let's recall how it was without Maven?

  1. To develop a project, you need to use a jar package written by others. We first download the open source jar package and put it in the lib directory of the project, and add this directory to the CLASSPATH (tell the Java execution environment, in which directories you can find you Class or package required by the Java program to be executed)
  2. We downloaded a.jar and found that a.jar also needs to depend on b.jar, and then we downloaded the b.jar package and started running
  3. If you are lucky enough, our project will be able to run properly after adding all the dependencies. If you are almost lucky, you will encounter version problems. For example, when a.jar calls b.jar, it finds that b.jar does not have this method at all. It is only available in other versions. Now it’s good. Just look for dependencies and adaptations. Version can take a lot of time
  4. And when we upload code to git, we must upload all these libs. When others download our code, they must also download the lib, which is really time-consuming

At this time Maven appeared as a package management tool in the Java world. Of course, there are other package management tools in the Java world, such as gradle. Just like yum is a package management tool in the Linux world and webpack is a package management tool in the front-end world

Types of Maven repositories

Insert picture description here
The process of Maven looking for a jar package is like this: first find it in the local warehouse, then go to the private server (if configured), and then go to the central warehouse (http://repo1.maven.org/maven2/, The maven team is responsible for maintenance)

After being found from the central warehouse, a copy will be placed in the private server and local warehouse, and a copy will also be placed in the local warehouse when found from the private server

After you have installed Maven, there is a settings.xml file in the conf directory. There are many configuration items in this file. This configuration file will be described in detail later.

<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->

There is this paragraph in this configuration file, saying that the default local warehouse address of Maven is ${user.home}/.m2/repository (of course you can reset the address of the local warehouse, the above is the template), I am a window computer , Take a look at this directory and
Insert picture description here
see that there are many jar packages stored locally. Of course, if you want to configure the private server, it is also configured in settings.xml. Just search for many tutorials and not repeat them.

There are many benefits to building a private server. In a company, some public basic components can be developed and placed on the private server for other colleagues to use.

Maven's default configuration

The overall structure of
Insert picture description here
a Maven project is like this. Why is the file structure of a Maven project like this?
This has to talk about a feature of Maven, convention is better than configuration.

Maven configures ${project.basedir}/src/main/java as the project's source code directory by default,
${project.basedir}/src/main/test as the project's test code directory,
${project.basedir}/target as the project Compile output directory, etc.

Spring Boot is the embodiment of convention over configuration. Think about it when we use spring mvc, we have to configure the view resolver and automatic package scanning. With the spring boot framework, we don’t need to configure it at all.

Detailed Maven project

The installation is quite simple, I will not introduce it, and I did not download it separately. Generally, I use the Maven that comes with Idea. The directory structure after downloading is as follows:
Insert picture description here

bin directory:
This directory contains scripts run by mvn. These scripts are used to configure java commands, prepare classpath and related Java system properties, and then execute Java commands.

Boot directory:
This directory contains only one file, which is plexus-classworlds-2.5.2.jar. Plexus-classworlds is a class loader framework. Compared with the default java class loader, it provides a richer syntax to facilitate configuration. Maven uses this framework to load its own class libraries.

conf directory:
This directory contains a very important file settings.xml. By directly modifying this file, you can customize the behavior of maven globally on the machine, which is effective for all users. In general, we prefer to copy the file to the ~/.m2/ directory (~ represents the user's home directory, under windows ~ is C:\Users\Peng, Peng is the user name of the editor), and then modify the file , Customize Maven's behavior at the user level.

Lib directory:
This directory contains all the Java class libraries needed for Maven runtime. Maven itself is developed in modules, so users can see files such as maven-core-3.0.jar, maven-model-3.0.jar, etc. In addition, there are also some third-party dependencies used by Maven, such as commons-cli-1.2.jar, commons-lang-2.6.jar and so on. ,

Detailed settings.xml configuration file

Let's talk about the settings.xml file in detail. This file can customize the behavior of Maven. As mentioned above, settings.xml can be placed in 2 locations, ~/.m2/setting.xml (not available by default, we need to copy it ourselves) And ${maven.home}/conf/setting.xml

The loading sequence of these two configuration files is ~/.m2/setting.xml>${maven.home}/conf/setting.xml, in order not to affect others, so we copy the settings.xml under conf to the home directory, in Customize the behavior of Maven at the user level.

Insert picture description here

This is similar to configuring environment variables. Both Windos and Linux can configure system-level environment variables and user-level environment variables. Let’s just talk about Linux. What is configured in /etc/profile is system-level environment variables. ~/.bash_profile is configured with user-level environment variables

There are quite a lot of various configuration items, set up mirror warehouse (Alibaba Cloud is more used in China), set up proxy, no more details

Maven commonly used commands

command description
mvn -version Display version information
mvn clean Delete target directory
mvn compile Compile the source code under src/main/java
mvn package Package, generate jar package or war package under target
mvn test Execute test cases of classes starting with Test or ending with Test under src/test/java
mvn install Package and copy the jar package or war package to the local warehouse for use by other modules
mvn deploy Publish the packaged files to private servers
mvn dependency:tree Print out the entire dependency tree of the project

Of course, you can also use
mvn clean package to clean up the package
mvn clean package -DskipTests=true to clean up the package and skip the test case
mvn clean install clean up the package, and copy the jar package or war package to the local warehouse

When running a single test, there is no need to test methods one by one. The mvn test command runs all test cases. It
should be noted that only test classes beginning or ending with Test will be executed. There is no need to write test classes by yourself. It is recommended to read the first article that demonstrates the method of quickly generating test classes, you can go and have a look, the generated test classes all end with Test

mvn dependency:tree> show.txt redirects dependency output to a file for easy viewing

Detailed pom.xml

groupId company domain name reversed
artifactId function command
version version number

These three dimensions determine a jar package, just like using (x, y, z) coordinates to uniquely determine a point in the three-dimensional space.

packaging packaging method, jar, war, maven-plugin (development of maven plugin)

Detailed scope

parameter Explanation Will it be entered into the final jar package
compile Default scope Yes
test Test use no
provided Compilation needs no
runtime Not required for compilation, required at runtime (separation of interface and implementation) Yes
system Load local jar no

Similar to the following, no scope is specified, indicating that the scope is compile

<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>

Test is used when running test cases, there is no need to enter the final jar, so the scope of the test framework you see is basically test

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

provided, it will be used when compiling, but will not be entered into the final jar package.
For example, if you want to run the spring boot project in tomcat as a war package, you must first add the following dependencies

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

Or you wrote a task to run on a Storm cluster or Flink cluster, and finally set the dependency of Storm or Flink to provided, because the cluster already has jar packages for these environments,

If you use the lombok plugin, you will find that lombok's Maven is in the following form, indicating that it will only be used during compilation.

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.16.6</version>
	<scope>provided</scope>
</dependency>

I wrote a test class as follows

@Data
public class Test {
    
    

    private String name;
    private int age;
}

The generated class file after decompilation is as follows, which verifies our idea. After compilation, it is indeed unnecessary to use the jar package of lombok

public class Test {
    
    
    private String name;
    private int age;

    public Test() {
    
    
    }

    public String getName() {
    
    
        return this.name;
    }

    public int getAge() {
    
    
        return this.age;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }
}

runtime, only used at runtime. For example, if your project has operations on the database, but does not add the corresponding JDBC implementation jar package, such as mysql-connector-java, it can be compiled successfully, and an error will only be reported at runtime. So you see that the scope of the jar package implemented by JDBC is runtime, indicating that this jar package will only be used at runtime

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.35</version>
	<scope>runtime</scope>
</dependency>

system, load jar locally, when you cooperate with a third-party company and they just give you a jar package, you have three choices

  1. mvn install to local warehouse
  2. mvn deploy to private server
  3. Specify the jar package path and load it locally, such as the following pom form
 <dependency>
	<groupId>com.tievd.third</groupId>
	<artifactId>arcvideo</artifactId>
	<version>1.0</version>
	<scope>system</scope>
	<systemPath>${basedir}/lib/face-api-1.0.jar</systemPath>
</dependency>

As mentioned above, the dependency of scope as system will not be entered into the final jar package. It is necessary to enter the dependency into the final jar package by configuring plug-ins, so this method is generally rarely used.

Dependency pass

Insert picture description here
Suppose we now have a multi-module project, and the dependency relationship is shown in the figure. When we introduce st-dal dependency in st-web module, st-common-lib dependency will also be introduced by us. This is dependency transfer, as listed in the following table The changes in the scope during the dependency process are listed, the column headings are the modules that are dependent, and the modules that each behavior depends on

compile test provided runtime
compile compile - - runtime
test test - - test
provided provided - provided provided
runtime runtime - - runtime

Welcome to follow

Insert picture description here

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/109019006