Table of Contents
SpringBoot Overview
Exploring the parent project
Program startup
After we finished the introductory case in the previous article, we can find that there is a big difference between the two:
The reason why the
Enter the
Select
生成工程
SpringBoot工程快速启动
问题引入
打包
启动
Home Java javaTutorial 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

May 11, 2023 am 09:25 AM
springboot

    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);
        }
    }
    Copy after login

    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 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&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><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

    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 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&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><div class="contentsignin">Copy after login</div></div>, 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="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/887/227/168376831282036.png" class="lazy" alt="How to solve the problems of SpringBoot official website construction and quick startup" /></p><h4 id="生成工程">生成工程</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>
    Copy after login

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

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

    启动

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

    java -jar 包名.jar
    Copy after login

    执行上述命令就可以看到 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 of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How Springboot integrates Jasypt to implement configuration file encryption How Springboot integrates Jasypt to implement configuration file encryption Jun 01, 2023 am 08:55 AM

    Introduction to Jasypt Jasypt is a java library that allows a developer to add basic encryption functionality to his/her project with minimal effort and does not require a deep understanding of how encryption works. High security for one-way and two-way encryption. , standards-based encryption technology. Encrypt passwords, text, numbers, binaries... Suitable for integration into Spring-based applications, open API, for use with any JCE provider... Add the following dependency: com.github.ulisesbocchiojasypt-spring-boot-starter2. 1.1Jasypt benefits protect our system security. Even if the code is leaked, the data source can be guaranteed.

    How SpringBoot integrates Redisson to implement delay queue How SpringBoot integrates Redisson to implement delay queue May 30, 2023 pm 02:40 PM

    Usage scenario 1. The order was placed successfully but the payment was not made within 30 minutes. The payment timed out and the order was automatically canceled. 2. The order was signed and no evaluation was conducted for 7 days after signing. If the order times out and is not evaluated, the system defaults to a positive rating. 3. The order is placed successfully. If the merchant does not receive the order for 5 minutes, the order is cancelled. 4. The delivery times out, and push SMS reminder... For scenarios with long delays and low real-time performance, we can Use task scheduling to perform regular polling processing. For example: xxl-job Today we will pick

    How to use Redis to implement distributed locks in SpringBoot How to use Redis to implement distributed locks in SpringBoot Jun 03, 2023 am 08:16 AM

    1. Redis implements distributed lock principle and why distributed locks are needed. Before talking about distributed locks, it is necessary to explain why distributed locks are needed. The opposite of distributed locks is stand-alone locks. When we write multi-threaded programs, we avoid data problems caused by operating a shared variable at the same time. We usually use a lock to mutually exclude the shared variables to ensure the correctness of the shared variables. Its scope of use is in the same process. If there are multiple processes that need to operate a shared resource at the same time, how can they be mutually exclusive? Today's business applications are usually microservice architecture, which also means that one application will deploy multiple processes. If multiple processes need to modify the same row of records in MySQL, in order to avoid dirty data caused by out-of-order operations, distribution needs to be introduced at this time. The style is locked. Want to achieve points

    How to solve the problem that springboot cannot access the file after reading it into a jar package How to solve the problem that springboot cannot access the file after reading it into a jar package Jun 03, 2023 pm 04:38 PM

    Springboot reads the file, but cannot access the latest development after packaging it into a jar package. There is a situation where springboot cannot read the file after packaging it into a jar package. The reason is that after packaging, the virtual path of the file is invalid and can only be accessed through the stream. Read. The file is under resources publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

    How to implement Springboot+Mybatis-plus without using SQL statements to add multiple tables How to implement Springboot+Mybatis-plus without using SQL statements to add multiple tables Jun 02, 2023 am 11:07 AM

    When Springboot+Mybatis-plus does not use SQL statements to perform multi-table adding operations, the problems I encountered are decomposed by simulating thinking in the test environment: Create a BrandDTO object with parameters to simulate passing parameters to the background. We all know that it is extremely difficult to perform multi-table operations in Mybatis-plus. If you do not use tools such as Mybatis-plus-join, you can only configure the corresponding Mapper.xml file and configure The smelly and long ResultMap, and then write the corresponding sql statement. Although this method seems cumbersome, it is highly flexible and allows us to

    Comparison and difference analysis between SpringBoot and SpringMVC Comparison and difference analysis between SpringBoot and SpringMVC Dec 29, 2023 am 11:02 AM

    SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

    How SpringBoot customizes Redis to implement cache serialization How SpringBoot customizes Redis to implement cache serialization Jun 03, 2023 am 11:32 AM

    1. Customize RedisTemplate1.1, RedisAPI default serialization mechanism. The API-based Redis cache implementation uses the RedisTemplate template for data caching operations. Here, open the RedisTemplate class and view the source code information of the class. publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations, BeanClassLoaderAware{//Declare key, Various serialization methods of value, the initial value is empty @NullableprivateRedisSe

    How to get the value in application.yml in springboot How to get the value in application.yml in springboot Jun 03, 2023 pm 06:43 PM

    In projects, some configuration information is often needed. This information may have different configurations in the test environment and the production environment, and may need to be modified later based on actual business conditions. We cannot hard-code these configurations in the code. It is best to write them in the configuration file. For example, you can write this information in the application.yml file. So, how to get or use this address in the code? There are 2 methods. Method 1: We can get the value corresponding to the key in the configuration file (application.yml) through the ${key} annotated with @Value. This method is suitable for situations where there are relatively few microservices. Method 2: In actual projects, When business is complicated, logic

    See all articles