SpringBoot初體驗之整合SpringMVC

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

  SpringBoot的特色: java

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

下面給你們介紹一下,SpringBoot整合SpringMVC的過程:spring

1、建立項目apache

一、使用IDEA建立一個Maven工程瀏覽器

 二、項目的相關信息填寫一下;安全

 三、點擊Finish,等待項目建立完成;springboot

2、項目依賴配置服務器

一、SpringBoot要求,項目要繼承SpringBoot的起步依賴spring-boot-starter-parent;app

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

二、同時整合SpringMVC,要導入web的啓動依賴;

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

三、導入座標後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>

3、編寫java代碼

一、編寫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); } }

二、編寫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!"; }; }

三、至此,SpriingBoot的簡單項目搭建完成,項目目錄結構以下;

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

 4、啓動項目,進行測試

一、啓動引導類,進行測試;

 二、當控制檯打印出以下信息時,證實SpringBoot項目啓動成功;

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

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

 

原文出處:https://www.cnblogs.com/xyfer1018/p/11509654.html

相關文章
相關標籤/搜索