A detailed explanation of the first spring boot project and dependency injection

Preface

Write a HelloWorld project manually, and have a deep understanding of the use of @ SpringBootApplication annotation

1. Project requirements:

The browser sends the Hello request, and the server receives and processes it in response to the Hello World string

2. Use IDEA to complete corresponding development

2.1 create a Maven project (jar)


2.2 import spring boot related dependencies in pom.xml (we will explain this configuration later)

<!--Import the corresponding parent project-->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
  </parent>
  <!--Import the corresponding web starter-->
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  </dependencies>

The directory structure after writing is as follows:

2.3 write a main program: start the Spring Boot application

package com.gs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication To mark a main program class, indicating that this is a Spring Boot application
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //Spring application starts
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

2.4 write related Controller classes

package com.gs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {

    @ResponseBody   //Hide the return value on the page
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

2.5 start the main program for testing


2.6 parsing our pom.xml file

1) Declare parent project

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

This dependency injection also has a parent project

 <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-dependencies</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

Click to enter the spring boot dependencies
You will find that it is used to really manage all versions of Spring Boot applications, that is, the so-called version Arbitration Center of Spring Boot. In the future, we need not write the version by default when importing dependency (but the dependency that is not managed in our dependencies naturally needs to declare the version number)

2) Starter

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

After reading the relevant official documents of Spring Boot, you will find that Spring Boot extracts all functional scenarios and makes them into starters. The starter launcher contains all the dependencies of the scenario. Therefore, the initiator of any scenario can be imported with any function.
Spring boot starter Web: the spring boot scenario launcher helps us import the components that the normal operation of the web module depends on;

The next section is a detailed explanation of our @ SpringBootApplication

43 original articles published, praised 2, visitors 2183
Private letter follow

Tags: Spring xml Maven

Posted on Fri, 14 Feb 2020 06:18:38 -0500 by xray_griff