springBoot study notes 1-helloworld

copy notes from mad god original article addresses

Introduction to SpringBoot

Review what is Spring

Spring is an open source framework, a lightweight in 2003 the rise of Java development framework, Author: Rod Johnson.

How Spring simplifies Java development

In order to reduce the complexity of Java development, Spring has adopted the following four key strategies:

1. Lightweight and minimally invasive programming based on POJO, everything is bean;

2. Realize loose coupling through IOC, dependency injection (DI) and interface-oriented;

3. Declarative programming based on aspects (AOP) and conventions;

4. Reduce style codes through aspects and templates, RedisTemplate, xxxTemplate;

What is SpringBoot

Students who have studied javaweb know that to develop a web application, from the very beginning to contact Servlet combined with Tomcat, run a Hello Wolrld program, it has to go through a lot of steps; later, I used the framework Struts, and then SpringMVC, and now SpringBoot, there will be other web frameworks in one or two years; have you experienced the continuous evolution of the framework, and then all the technologies of your own development projects are constantly changing and transforming? You can go through the suggestions;
get back to the subject , what is SpringBoot, it is a javaweb development framework, similar to SpringMVC, compared with the benefits of other javaweb frameworks, the official said is to simplify the development, the agreement is greater than the configuration, you can "just run", can "just run" Develop web applications quickly, and develop an http interface with a few lines of code.
The development of all technical frameworks seems to follow a main line rule: a standard framework derived from a complex application scenario, people only need to perform various configurations without having to implement it by themselves. At this time, the powerful configuration function becomes an advantage; After development to a certain extent, people selected practical functions and design essences according to the actual production and application conditions, and reconstructed some lightweight frameworks; later, in order to improve development efficiency, the original various configurations were too troublesome, so they began to advocate " "Convention is greater than configuration", which leads to some one-stop solutions.
Yes, this is the process of Java Enterprise Application->J2EE->spring->springboot.
With the continuous development of Spring, more and more fields are involved. Project integration and development need to cooperate with various documents, which gradually become less easy to use and simple, contrary to the original concept, and even called configuration hell. Spring Boot is a development framework abstracted under such a background. The purpose is to make it easier for everyone to use Spring and integrate various commonly used middleware and open source software;
Spring Boot is developed based on Spring. Spirng Boot itself does not provide the core features and extended functions of the Spring framework. It is only used to quickly and agilely develop a new generation of applications based on the Spring framework. In other words, it is not a solution to replace Spring, but a tool that is closely integrated with the Spring framework to enhance the Spring developer experience. With the core idea of ​​convention greater than configuration, Spring Boot has helped us make a lot of settings by default. Most Spring Boot applications require very little Spring configuration. At the same time, it integrates a large number of commonly used third-party library configurations (such as Redis, MongoDB, Jpa, RabbitMQ, Quartz, etc.). These third-party libraries in Spring Boot applications can be used almost out of the box with zero configuration.
To put it simply, SpringBoot is not a new framework. It is configured with many frameworks by default, just like maven integrates all jar packages, and spring boot integrates all frameworks.
Spring Boot was born and stood at a relatively high starting point. After several years of development, the ecology is sufficiently complete, and Spring Boot has become the hottest technology in the Java field.
The main advantages of Spring Boot:

  • Get started faster for all Spring developers

  • Out of the box, various default configurations are provided to simplify project configuration

  • Embedded container simplifies web projects

  • No redundant code generation and XML configuration requirements

It's really cool, let's quickly experience the feeling of developing an interface!

Hello,World

Preparations
We will learn how to quickly create a Spring Boot application and implement a simple Http request processing. Through this example, have a preliminary understanding of Spring Boot, and experience its simple structure and rapid development features.

My environment preparation:

java version “1.8.0_181”

Maven-3.6.1

SpringBoot 2.x latest version

development tools:

IDEA

Create a basic project description

Spring officially provides a very convenient tool for us to quickly build applications

Spring Initializr:https://start.spring.io/

Project creation method 1: Use Spring Initializr's Web page to create a project

1. Open https://start.spring.io/

2. Fill in the project information

3. Click the "Generate Project" button to generate the project; download the project
Insert picture description here

4. Unzip the project package and use IDEA to import it as a Maven project. You can take the next step all the way until the project is imported.

5. If it is the first time to use, the speed may be slower, there are more packages, and you need to wait patiently for everything to be ready.

Project creation method 2: Use IDEA to directly create a project

1. Create a new project

2. Choose spring initalizr, you can see that the default is to go to the quick build tool on the official website to implement
Insert picture description here

3. Fill in the project information

4. Select the components to be initialized (you can check Web for beginners)

5. Fill in the project path

6. Wait for the project to build successfully

Project structure analysis:

Complete the creation of the basic project through the above steps. The following files will be automatically generated.

1. The main startup class of the program

2. An application.properties configuration file

3. A test class

4. A pom.xml
Insert picture description here

Analysis of pom.xml
Open pom.xml and look at the dependencies of the Spring Boot project:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--web依赖: tomcat, dispatcherServlet,xml...-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

         <!--spring-boot-starter-test 从中观察 得到 所有的springboot依赖都是使用spring-boot-starter这个开头的-->
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--打jar包 工具-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

You can directly modify the port number of the web project in the application.properties file under the resources folder
Insert picture description here

Write an http interface
1. Create a new controller package in the same level directory of the main program, and it must be in the same level directory, otherwise it will not be recognized

2. Create a new HelloController class in the package

package com.example.demo1.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    
    @RequestMapping("/hello")
    public String hello(){
    
    
        //调用业务,接受前端数据
        return "hello,World";
    }
}

3. After writing, start the project from the main program, the browser initiates a request, and the page returns; the console outputs the port number accessed by Tomcat!
Insert picture description here
In a few simple steps, the development of a web interface is completed. SpringBoot is that simple. So we often use it to build our microservice projects!

Type the project into a jar package and click on the maven package

Insert picture description here
If the packaging is successful, a jar package will be generated in the target directory. After the jar package is
Insert picture description here
printed, it can be run anywhere! OK
Insert picture description here

Easter eggs

How to change the letters spelled by the characters displayed at startup, what about SpringBoot? That is, the banner pattern;

Just one step: Go to the resources directory under the project and create a new banner.txt.

The patterns can be found at: Bootschool this website is generated, and then copied to the file!
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44788518/article/details/113955111