SpringBoot
is a brand new framework provided by the Pivotal team and is designed to be used with To simplify the initial construction and development process of Spring applications.
Everyone has experienced the SpringBoot
program. Let’s look back and see what the main function of SpringBoot
is to simplify the construction and development process of Spring
.
OriginalSpring
There are the following problems in environment construction and development:
Cumbersome configuration dependencies
Settings Cumbersome
SpringBoot
The advantage of the program happens to be automatic configuration for the shortcomings of Spring
. This is used to solve the problem of cumbersome program configuration in Spring
Spring the problem of cumbersome program dependency settings
SpringBoot program, we neither used the local
tomcat nor the
tomcat plug-in, but used the
SpringBoot built-in server.
SpringBoot
Spring Initializr The
pom.xml configuration file of the
Maven project created in the
method automatically generates many dependencies containing starter
, as shown below
These dependencies are startup dependencies. Next, let’s explore how they are implemented.
From the above file, we can see that a parent project is specified. We enter the parent project and find that another parent project is specified in the parent project, as shown in the figure below
Then enter the parent project. In this project, we can see the configuration content structure as shown below
The properties
tag in the above picture defines the versions that each technical software depends on, which avoids us considering version compatibility issues when using different software technologies. In properties
we find the versions of servlet
and mysql
as shown below
dependencyManagement# The ## tag is for dependency version locking, but the corresponding dependency is not imported; if our project needs that dependency, we only need to introduce the
groupid and
artifactId of the dependency. There is no need to define the
version .
build tag also locks the plug-in version, as shown below
After configuring pom.xml, it is not difficult to understand why none of our project’s dependencies are configured
version.
pom.xml in the project we created
pom.xml and you will find that it introduces the following dependencies
spring-web and
spring-webmvc are dependent on each other. This is why our project can still use the annotations in
springMVC without relying on these two packages.
spring-boot-starter-tomcat. From the name, we can basically confirm that
tomcat is internally dependent, so our project can start normally.
starter
Common project names in SpringBoot define all project coordinates used by the current project to reduce dependency configuration
parent
SpringBoot projects define several coordinate version numbers (dependency management, not dependency) to reduce dependencies Conflicting Purpose
spring-boot-starter-parent
(2.5.0) and spring-boot-starter-parent
(2.4.6) have a total of 57 coordinates Different versions
Actual development
When using arbitrary coordinates, only write G and A in GAV, V is provided by SpringBoot
G:groupid
A:artifactId
V:version
If a coordinate error occurs, specify the version again (be careful of version conflicts)
Every SpringBoot
program created contains a class similar to the following. We call this class the boot class
@SpringBootApplication public class Springboot01QuickstartApplication { public static void main(String[] args) { SpringApplication.run(Springboot01QuickstartApplication.class, args); } }
Note:
SpringBoot
When creating a project, use the jar packaging method
SpringBoot
The boot class is the entry point of the project. Running the main
method can start the project
because we configured ## in pom.xml
#spring-boot-starter-web depends on it, and this dependency knows through previous learning that it depends on
tomcat, so running the
main method can use
tomcat Start our project.
tomcat server, can we use
jetty# instead of tomcat
## Server, jetty
When we maven
are advanced, we will talk about the server used by maven
private servers. To switch the web
server, you need to exclude the default tomcat
server. How to exclude it? Using the exclusion
tag <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:xml;"><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency></pre><div class="contentsignin">Copy after login</div></div>
Now can we run the bootstrap class? Try running it. The printed log information is as follows
The program stopped directly. Why? That's because if the
tomcat server is excluded, there will be no server in the program. So at this time not only the tomcat
server must be excluded, but also the jetty
server must be introduced. In pom.xml
, because the starting dependency of jetty
is <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:xml;"><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency></pre><div class="contentsignin">Copy after login</div></div>
, run the boot class again. You can see in the log information that
Server
Summary:By switching the server, it is not difficult for us to find that we are using SpringBoot
Comparison between spring and springboot
When changing technology, you only need to import the starting dependencies of the technology.
Coordinates
The coordinates in the program need to be written by yourself, and there are many coordinates
The coordinates in the program are automatically generated by checking when we create the project
The program needs to write this configuration class by itself. Everyone has written this configuration class before, and it must feel very complicated
The program does not need to be written by ourselves
The configuration class of the program needs to be written by yourself. The SpringBoot
program does not need to be written.
Spring Initializrbased on Idea requires an Internet connection to quickly build the
Official website construction projectSpringBoot
project.
project can be quickly built in the entry case is because Idea
uses the provided by the official website Quickly build the components of the SpringBoot
project. So how to build projects on the official website? Build through the following stepsEnter the SpringBoot official website
official website and drag it to the bottom to see the following content
Then click
Spring Initializr The hyperlink will jump to the following page
Does the content of this page look familiar? The interface is basically the same as the one we use
Idea to quickly build the SpringBoot
project. Enter the corresponding information on the above pageSelect dependencies
You can click the ADD DEPENDENCIES... CTRL B
button in the upper right corner of the picture above , the following interface will appear<p><img src="https://img.php.cn/upload/article/000/887/227/168376831282036.png" alt="How to solve the problems of SpringBoot official website construction and quick startup" /></p><h4>生成工程</h4><p>以上步骤完成后就可以生成 <code>SpringBoot
工程了。在页面的最下方点击 GENERATE CTRL + 回车
按钮生成工程并下载到本地,如下图所示
打开下载好的压缩包可以看到工程结构和使用 Idea
生成的一模一样,如下图
而打开 pom.xml
文件,里面也包含了父工程和 Spring Web
的依赖。
通过上面官网的操作,我们知道 Idea
中快速构建 SpringBoot
工程其实就是使用的官网的快速构建组件,那以后即使没有 Idea
也可以使用官网的方式构建 SpringBoot
工程。
以后我们和前端开发人员协同开发,而前端开发人员需要测试前端程序就需要后端开启服务器,这就受制于后端开发人员。为了摆脱这个受制,前端开发人员尝试着在自己电脑上安装 Tomcat
和 Idea
,在自己电脑上启动后端程序,这显然不现实。
我们后端可以将 SpringBoot
工程打成 jar
包,该 jar
包运行不依赖于 Tomcat
和 Idea
这些工具也可以正常运行,只是这个 jar
包在运行过程中连接和我们自己程序相同的 Mysql
数据库即可。这样就可以解决这个问题,如下图
那现在问题是如何打包呢?
由于我们在构建 SpringBoot
工程时已经在 pom.xml
中配置了如下插件
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
所以我们只需要使用 Maven
的 package
指令打包就会在 target
目录下生成对应的 Jar
包。
注意:该插件必须配置,不然打好的
jar
包也是有问题的。
进入 jar
包所在位置,在 命令提示符
中输入如下命令
java -jar 包名.jar
执行上述命令就可以看到 SpringBoot
运行的日志信息
The above is the detailed content of How to solve the problems of SpringBoot official website construction and quick startup. For more information, please follow other related articles on the PHP Chinese website!