Home > Java > javaTutorial > body text

What are the basic operations and concepts of getting started with SpringBoot?

WBOY
Release: 2023-05-11 12:13:05
forward
999 people have browsed it

    1. What is Spring Boot

    Why should you learn Spring Boot?

    Spring was born to simplify the development of Java programs, and Spring Boot was born to simplify the development of Spring programs.

    Spring Boot is the scaffolding of the Spring framework. It was born for the rapid development of the Spring framework.

    2. Spring Boot Advantages

    • Quickly integrates frameworks. Spring Boot provides the function of starting to add dependencies, which is used to integrate various frameworks in seconds.

    • Built-in running container, no need to configure Web containers such as Tomcat, run and deploy programs directly.

    • Rapidly deploy projects to get your projects up and running without the need for external containers.

    • You can completely abandon the cumbersome XML and use annotations and configuration for development.

    • Supports more monitoring indicators to better understand the running status of the project.

    3.Spring Boot project creation

    • Create using Idea [provided by ide developer]

    • Web version creation method [Officially provided by Spring]

    ##3.1 Create using Idea

    The IDEA version is 2021.2.2

    What are the basic operations and concepts of getting started with SpringBoot?

    Because of our Idea community version (other versions are also applicable), you must first install the Spring Assistant (Spring Assistant) plug-in before you can create a Spring Boot project, as shown in the following figure:

    What are the basic operations and concepts of getting started with SpringBoot?

    What are the basic operations and concepts of getting started with SpringBoot?

    After installation, there is a Spring Assistant option, as shown in the figure below:

    What are the basic operations and concepts of getting started with SpringBoot?

    3.2 Spring Boot project

    What are the basic operations and concepts of getting started with SpringBoot?

    What are the basic operations and concepts of getting started with SpringBoot?

    https://start.aliyun.com

    What are the basic operations and concepts of getting started with SpringBoot?

    What are the basic operations and concepts of getting started with SpringBoot?

    What are the basic operations and concepts of getting started with SpringBoot?

    Click Finish to complete the Spring Boot project creation.

    Note:

    It takes a long time to load the Spring Boot item for the first time, because the current Spring Boot framework is not in its own local warehouse.

    In order to speed up the download of the Spring Boot project, before opening the project, please confirm that Maven has been configured as a domestic source

    3.3 Start and verify whether the Spring Boot project has been created successfully

    What are the basic operations and concepts of getting started with SpringBoot?

    What are the basic operations and concepts of getting started with SpringBoot?

    3.4 Web version creation (understanding)

    You can also create a Spring Boot project without using Idea. We can use the official version provided by Spring Web version to create a Spring Boot project.

    To create a project for the web version, first visit: https://start.spring.io, as shown in the figure below:

    What are the basic operations and concepts of getting started with SpringBoot?

    What are the basic operations and concepts of getting started with SpringBoot?

    Click the Generate button to download a Spring Boot zip package. After unzipping the zip, the directory is as follows:

    What are the basic operations and concepts of getting started with SpringBoot?

    After opening it with Idea, the Spring Boot item will be created successfully. As shown in the figure below:

    What are the basic operations and concepts of getting started with SpringBoot?

    4. Project directory introduction and application

    What are the basic operations and concepts of getting started with SpringBoot?

    The newly created Spring Boot project directory is as follows:

    What are the basic operations and concepts of getting started with SpringBoot?

    The Spring Boot project has two main directories:

    src/main/java is the Java source code.

    src/main/resources is static resources or configuration files:

    /static: static resource folder;

    /templates: template resource folder.

    4.1 Project operation

    Click the main method of the startup class to run the Spring Boot project. The successful startup is as shown in the following figure:

    What are the basic operations and concepts of getting started with SpringBoot?

    4.2 Output Hello world

    We learn JavaEE to implement Web projects or interfaces. Before, Spring was actually an ordinary Java project, and there was no way to interact directly with the browser, so next we have to use Spring Boot is used to interact with browsers and users.

    Create the HelloController file under the created project package path. The implementation code is as follows:

    package com.example.demo;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    @Controller
    @RequestMapping("/hi")//路由映射
    public class HelloController {
        @RequestMapping("/index")//路由映射
        @ResponseBody//返回一个非静态页面的数据
        public String sayHi(){
            return "你好,Spring Boot";
        }
    }
    Copy after login

    Restart the project and visit http://localhost:8080/hi/index. The final effect is as follows:

    What are the basic operations and concepts of getting started with SpringBoot?

    5. Note - Package path error

    We try to move HelloController to other packages, such as the following methods:

    What are the basic operations and concepts of getting started with SpringBoot?

    Run our project and found that the program reported an error, as shown in the following figure:

    What are the basic operations and concepts of getting started with SpringBoot?

    This means that the Spring Boot project did not inject objects into the container middle.

    5.1 Correct path

    When we put the container class and startup class to be injected into the same directory, as shown in the following figure:

    What are the basic operations and concepts of getting started with SpringBoot?

    At this time, the Spring Boot project can normally inject beans into the container.

    5.2 Summary

    Convention is greater than configuration

    The above situation reflects another feature of the Spring Boot project: convention is greater than configuration.

    We can also see this feature in Spring projects. For example, in Spring, the scanning path of beans must be configured, but in Spring Boot, this is not required. The Spring configuration is as follows:

    What are the basic operations and concepts of getting started with SpringBoot?

    Note:

    The classes annotated in the 5 major categories must be placed in the same directory as the startup class or in a subdirectory of the startup class, otherwise they will not be recognized

    The above is the detailed content of What are the basic operations and concepts of getting started with SpringBoot?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!