Introduction and use of Spring Boot

Posted by shiznatix on Sat, 05 Feb 2022 07:01:04 +0100

catalogue

preface

Evolution of Java backend

What is Spring boot?

text

Getting started with Spring boot

summary

raise a query

How to effectively learn methods and technical consultants

preface

Evolution of Java backend

In today's era when the Internet is so developed, the amount of data generated every day is countless. The back-end is the core part of a program, so a rapid construction of the back-end can often improve the efficiency of the whole program. As a programmer of Java language back-end development, I must be familiar with spring. In the early days of Java back-end, everyone used servlet+jsp to build single applications. Later, ssh(struts+spring+hibernate) and ssm(Spring+Spring+Mybatis) frameworks were produced, which improved the development efficiency of the back-end a lot, but there were a lot of cumbersome configurations. Now programmers don't need to focus on the cumbersome aspect of business configuration until the birth of boot. So today, I'll bring you the use of spring boot.

What is Spring boot?

Original text of official website:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

Translation:

Spring Boot can easily create independent, production level, spring based applications that you can "run directly".

 

We have an opinionated view of the Spring platform and third-party libraries so that you can start with the least hassle. Most Spring bootstrap applications require a minimum of Spring configuration.

I understand:

In the simplest terms, Springboot is a scaffold based on Spring. It automatically selects and configures the basic environment needed by programmers. As long as you reference the environment of a scaffold, it automatically configures it for you. Let programmers spend more time on business logic.

text

Getting started with Spring boot

Use maven to build the Spring boot project.

The pom file is as follows. We have used version 2.2.1, which is the most used so far.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springbootTest</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.1.RELEASE</version>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

</project>

Any program has an entry, that is, the startup class. We create the project structure and startup class.

Start the code in the class

// Code in Application startup class


/**
 * @Author liha
 * @Date 2022-02-05 12:59
 * Liha YYDS
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class,args);

    }
}

At this point, we can start the test! Right click the startup class to run directly.

This means that the operation is successful!

We can try to write the interface and call it.

// Created under controller package


/**
 * @Author liha
 * @Date 2022-02-05 13:01
 * Liha YYDS
 */
@RestController
public class SpringController {


    @RequestMapping("/test")
    public String test(){
        System.out.println("this is hello world in Spring Boot.");
        return "this is hello world in Spring Boot.";
    }
}

 @RestController:

@ Controller: register in the Spring container and give it to Spring management

@ ResponseBody: indicate on the class that this annotation indicates that all methods in this class return Json type.

@RequestMapper:

The annotation of spring MVC indicates that the request path is mapped to this method.

After writing the code of the controller layer, we restart our startup class.

After successful startup, we open the browser and enter the following address in the address bar to find that the return value of the interface method we wrote in the controller layer is displayed.

http://localhost:8080/test

// The default port of tomcat is 8080, and the default port can also be changed.

At this time, we have successfully completed the hello world of Spring Boot.

summary

This post explains the evolution history of Java backend and what Spring boot scaffolding is.

Use Spring boot to write the universal hello world code.

raise a query

Why do bloggers think they can always mention it as programmers? Then try your best to solve your why, then "on the road".

With so many configuration files in ssm, why do I use Spring boot to build a project without writing a configuration file, and write a startup class directly to run the project?

Yes, the answers to why are in the @ SpringBootApplication annotation of the startup class.

The next post will carefully explain how the underlying source code of @ SpringBootApplication can avoid writing a configuration file.

How to effectively learn methods and technical consultants

Yes, you're disappointed. I'm not advertising.

I write this post just to analyze the experience of programmers in recent years, the use of popular frameworks and source code interpretation, as well as the sharing of some common bug s and tool scripts.

Bloggers think they are ordinary people, and I think most of their friends are ordinary people, but why do some people learn programming so quickly and efficiently? Yes, I think it's experience. I'd like to share with you the experience of several years, how to improve learning efficiency and what to learn at what stage, so that you can avoid detours. In addition, you can share the bugs you encounter with me and solve them together. I think I'm learning to solve one bug after another.

Topics: Java IntelliJ IDEA Spring Spring Boot Back-end