SpringBoot's first experience integrating SpringMVC

  As a developer, we all know that Spring Boot is designed based on Spring 4.0, which not only inherits the original excellent features of the Spring framework, but also further simplifies the entire construction and development process of Spring applications by simplifying the configuration. In addition, SpringBoot integrates a large number of frameworks, so that the version conflicts of dependent packages and the instability of references have been well resolved.

  Features of SpringBoot: 

    Faster onboarding experience for Spring-based development
    Out of the box, no code generation and no XML configuration required. At the same time, the default value can also be modified to meet specific needs . It
    provides some common non-functional features in large-scale projects, such as embedded server, security, indicators, health detection, external configuration, etc.
    SpringBoot is not an enhancement of Spring functions, but Provides a fast way to use Spring

Let me introduce to you the process of SpringBoot integrating SpringMVC:

1. Create a project

1. Use IDEA to create a Maven project

 2. Fill in the relevant information of the project;

 3. Click Finish and wait for the project to be created;

2. Project dependency configuration

1. SpringBoot requires that the project should inherit SpringBoot's starting dependency spring-boot-starter-parent;

 <!--SpringBoot的起步依赖spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

2. Integrate SpringMVC at the same time, and import web startup dependencies;

 <dependencies>
        <!--web的启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3. After importing the coordinates, the pom.xml file is:

<?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>
    <!--SpringBoot的起步依赖spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <groupId>com.xyfer</groupId>
    <artifactId>springbootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--web的启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

3. Write java code

1. Write the bootstrap class of SpringBoot to start the SpringBoot project;

package com.xyfer;

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

@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class);
    }

}

2. Write the code of the Controller layer;

package com.xyfer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public  class DemoController {
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "Hello SpringBoot!";
    };
}

3. So far, the simple project of SpriingBoot is completed, and the project directory structure is as follows;

 SpringBoot scans the boot class MySpringBootApplication.java sibling package and its subpackages by default, so the controller layer can be scanned;

 4. Start the project and test it

1. Start the bootstrap class and test it;

 2. When the console prints the following information, it proves that the SpringBoot project has been successfully started;

 3. Enter the address in the browser: http://localhost:8080/test, the access is successful!

So far, a simple SpringBoot project has been built!

 

Reprinted in: https://www.cnblogs.com/xyfer1018/p/11509654.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568822&siteId=291194637