SpringBoot初體驗之整合SpringMVC


  作為開發人員,大家都知道,SpringBoot是基於Spring4.0設計的,不僅繼承了Spring框架原有的優秀特性,而且還通過簡化配置來進一步簡化了Spring應用的整個搭建和開發過程。另外SpringBoot通過集成大量的框架使得依賴包的版本沖突,以及引用的不穩定性等問題得到了很好的解決。

  SpringBoot的特點: 

    為基於Spring的開發提供更快的入門體驗
    開箱即用,沒有代碼生成,也無需XML配置。同時也可以修改默認值來滿足特定的需求
    提供了一些大型項目中常見的非功能性特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等
    SpringBoot不是對Spring功能上的增強,而是提供了一種快速使用Spring的方式

下面給大家介紹一下,SpringBoot整合SpringMVC的過程:

一、創建項目

1、使用IDEA創建一個Maven工程

 2、項目的相關信息填寫一下;

 3、點擊Finish,等待項目創建完成;

二、項目依賴配置

1、SpringBoot要求,項目要繼承SpringBoot的起步依賴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、同時整合SpringMVC,要導入web的啟動依賴;

 <dependencies>
        <!--web的啟動依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3、導入坐標后pom.xml文件為:

<?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>

三、編寫java代碼

1、編寫SpringBoot的引導類,以便啟動SpringBoot項目;

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、編寫Controller層的代碼;

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、至此,SpriingBoot的簡單項目搭建完成,項目目錄結構如下;

 SpringBoot默認掃描引導類MySpringBootApplication.java同級包及其子包,所以controller層能被掃描到;

 四、啟動項目,進行測試

1、啟動引導類,進行測試;

 2、當控制台打印出如下信息時,證明SpringBoot項目啟動成功;

 3、在瀏覽器輸入地址:http://localhost:8080/test,訪問成功!

至此,一個簡單的SpringBoot項目搭建完成!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM