Home  >  Article  >  Java  >  How to solve the problems of SpringBoot official website construction and quick startup

How to solve the problems of SpringBoot official website construction and quick startup

WBOY
WBOYforward
2023-05-11 09:25:051623browse

    SpringBoot Overview

    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

    • ##starting dependencies. This is used to solve

      Spring the problem of cumbersome program dependency settings

    • auxiliary functions (built-in server,...). When we started the

      SpringBoot program, we neither used the local tomcat nor the tomcat plug-in, but used the SpringBoot built-in server.

    Next let’s talk about the starting dependencies of

    SpringBoot

    Starting dependencies

    We use

    Spring Initializr The pom.xml configuration file of the Maven project created in the method automatically generates many dependencies containing starter, as shown below

    How to solve the problems of SpringBoot official website construction and quick startup

    These dependencies are startup dependencies. Next, let’s explore how they are implemented.

    Exploring the parent project

    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

    How to solve the problems of SpringBoot official website construction and quick startup

    Then enter the parent project. In this project, we can see the configuration content structure as shown below

    How to solve the problems of SpringBoot official website construction and quick startup

    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

    How to solve the problems of SpringBoot official website construction and quick startup

    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 .

    The

    build tag also locks the plug-in version, as shown below

    How to solve the problems of SpringBoot official website construction and quick startup

    After reading the parent project

    After configuring pom.xml, it is not difficult to understand why none of our project’s dependencies are configured version.

    Exploring dependencies
    The following dependencies are configured in

    pom.xml in the project we created

    How to solve the problems of SpringBoot official website construction and quick startup

    Enter the dependency and check the dependencies of

    pom.xml and you will find that it introduces the following dependencies

    How to solve the problems of SpringBoot official website construction and quick startup

    which introduces

    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.

    and relies on

    spring-boot-starter-tomcat. From the name, we can basically confirm that tomcat is internally dependent, so our project can start normally.

    Conclusion: If you need to use technology in the future, you only need to introduce the starting dependencies corresponding to the technology

    Summary

    starter

    • Common project names in SpringBoot define all project coordinates used by the current project to reduce dependency configuration

    parent

    • The projects to be inherited by all

      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)

    Program startup

    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.

    Switch web server

    Now we start the project using the

    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 <pre class="brush:xml;">&lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;exclusions&gt; &lt;exclusion&gt; &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;/exclusion&gt; &lt;/exclusions&gt; &lt;/dependency&gt;</pre> Now can we run the bootstrap class? Try running it. The printed log information is as follows

    How to solve the problems of SpringBoot official website construction and quick startupThe 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 <pre class="brush:xml;">&lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-jetty&lt;/artifactId&gt; &lt;/dependency&gt;</pre>, run the boot class again. You can see in the log information that

    is used. jetty

    Server

    How to solve the problems of SpringBoot official website construction and quick startup

    Summary:

    By switching the server, it is not difficult for us to find that we are using SpringBoot
    When changing technology, you only need to import the starting dependencies of the technology.

    Comparison between spring and springboot

    After we finished the introductory case in the previous article, we can find that there is a big difference between the two:

    How to solve the problems of SpringBoot official website construction and quick startup

    Coordinates

    • Spring

      The coordinates in the program need to be written by yourself, and there are many coordinates

    • SpringBoot

      The coordinates in the program are automatically generated by checking when we create the project

    web3.0 configuration class

    • Spring

      The program needs to write this configuration class by itself. Everyone has written this configuration class before, and it must feel very complicated

    • SpringBoot

      The program does not need to be written by ourselves

    Configuration Class

    • Spring/SpringMVC

      The configuration class of the program needs to be written by yourself. The SpringBoot program does not need to be written.

    Note: The
    Spring Initializr

    based on Idea requires an Internet connection to quickly build the SpringBoot project.

    Official website construction project

    The reason why the

    SpringBoot

    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

    Enter the

    SpringBoot

    official website and drag it to the bottom to see the following content

    How to solve the problems of SpringBoot official website construction and quick startupThen click

    Spring Initializr

    The hyperlink will jump to the following page

    How to solve the problems of SpringBoot official website construction and quick startupDoes 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

    Select

    Spring Web

    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 + 回车 按钮生成工程并下载到本地,如下图所示

    How to solve the problems of SpringBoot official website construction and quick startup

    打开下载好的压缩包可以看到工程结构和使用 Idea 生成的一模一样,如下图

    How to solve the problems of SpringBoot official website construction and quick startup

    而打开 pom.xml 文件,里面也包含了父工程和 Spring Web 的依赖。

    通过上面官网的操作,我们知道 Idea 中快速构建 SpringBoot 工程其实就是使用的官网的快速构建组件,那以后即使没有 Idea 也可以使用官网的方式构建 SpringBoot 工程。

    SpringBoot工程快速启动

    问题引入

    How to solve the problems of SpringBoot official website construction and quick startup

    以后我们和前端开发人员协同开发,而前端开发人员需要测试前端程序就需要后端开启服务器,这就受制于后端开发人员。为了摆脱这个受制,前端开发人员尝试着在自己电脑上安装 TomcatIdea ,在自己电脑上启动后端程序,这显然不现实。

    我们后端可以将 SpringBoot 工程打成 jar 包,该 jar 包运行不依赖于 TomcatIdea 这些工具也可以正常运行,只是这个 jar 包在运行过程中连接和我们自己程序相同的 Mysql 数据库即可。这样就可以解决这个问题,如下图

    How to solve the problems of SpringBoot official website construction and quick startup

    那现在问题是如何打包呢?

    打包

    由于我们在构建 SpringBoot 工程时已经在 pom.xml 中配置了如下插件

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

    所以我们只需要使用 Mavenpackage 指令打包就会在 target 目录下生成对应的 Jar 包。

    注意:该插件必须配置,不然打好的 jar 包也是有问题的。

    启动

    进入 jar 包所在位置,在 命令提示符 中输入如下命令

    java -jar 包名.jar

    执行上述命令就可以看到 SpringBoot 运行的日志信息

    How to solve the problems of SpringBoot official website construction and quick startup

    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!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete