Spring Boot框架搭建

 

目錄

一、Spring Boot概述

二、Spring Boot的優點

三、Spring Boot框架搭建


一、Spring Boot概述

Spring Boot 是 Spring 框架的一個新的子項目,用於創建 Spring 4.0 項目。它可以自動配置 Spring 的各種組件,並不依賴代碼生成和 XML 配置文件。Spring Boot 也提供了對於常見場景的推薦組件配置。Spring Boot 可以大大提升使用 Spring 框架時的開發效率。

二、Spring Boot的優點

  • 輕鬆創建獨立的Spring應用程序。
  • 內嵌Tomcat、jetty等web容器,不需要部署WAR文件。
  • 提供一系列的“starter” 來簡化的Maven配置,不需要添加很多依賴。
  • 開箱即用,儘可能自動配置Spring。

三、Spring Boot框架搭建

第一步:創建一個maven jar項目。

 

 

 

 

第二步:在pom.xml添加spring-boot-starter-web依賴。

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

  <groupId>com.hedong</groupId>
  <artifactId>SpringBootDemo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>SpringBootDemo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <!--spring boot 父依賴-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>
  <dependencies>
    <!--spring boot 配置web依賴-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

第三步:在pom.xml文件上右鍵選擇maven→reimport導入依賴,完成後左邊External Libraries下將會自動導入很多關於Spring的依賴。

 

 

第四步:寫一個控制器如下,然後寫一個main方法把程序跑起來

HelloController.java

package com.hedong.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController//相當於聲明Controller - 提共restful 風格
@EnableAutoConfiguration//自動配置,不需要寫spring的配置文件
public class HelloController {
    @RequestMapping("/hello")//映射路徑
    @ResponseBody//響應體
    public String hello() {
        return "Hello World";
    }

    public static void main(String[] args) {
        //啓動程序
        SpringApplication.run(HelloController.class, args);
    }
}

第五步:點擊圖中綠色三角形啓動程序,在瀏覽器中訪問http://localhost:8080/hello

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章