Spring-boot入門

一、認識Spring-boot

1、Spring-boot的簡單介紹

  • 是簡化Spring應用開發的一個框架
  • 是對整個Spring技術棧的一個大整合
  • J2EE開發的一站式解決方案

2、初識微服務

  • 它是一種架構風格
  • 微服務架構是以一組小服務來開發單個應用程序的方法,每一個服務運行在自己獨立的進程中並且使用輕量級的方法通信,通常是一個HTTP API接口
  • 每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元

二、快速入門

1、配置Maven環境

  在Maven的xxxx/conf/setting.xml文件中的profiles標籤添加以下內容,以統一JDK版本

<profile> 
    <id>jdk‐1.8</id>
    <activation> 
          <activeByDefault>true</activeByDefault> 
          <jdk>1.8</jdk>
    </activation> 
<properties> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target>
     <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
   </properties> 
</profile>

2、IDEA中的設置

  在IDEA中引入本地Maven:File-settings-Maven
在這裏插入圖片描述

3、第一個Spring boot應用

項目結構
在這裏插入圖片描述

3.1 創建一個maven項目

3.2 配置pom.xml文件

  在pom.xml文件中導入以下依賴:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>Spring boot的版本</version>
    </parent>

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

3.3 編寫一個主程序,用來啓動Spring應用

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

/**
 * @SpringBootApplication 標註一個主程序類,說明這是一個Spring boot應用
 **/
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //啓動spring應用
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

3.4 編寫相關controller、service

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

@Controller
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello,Spring!";
    }
}

3.5 運行主程序

3.6 簡化部署

1、在pom.xml文件中添加如下內容:

    <build>
        <plugins>
            <!--可以將應用打包成一個可執行的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
2、點擊IDEA右側的Maven,打開如下,運行package,將項目打成一個可執行的jar包。在這裏插入圖片描述

該jar包的位置在項目的target目錄下
在這裏插入圖片描述

3、運行

   將jar包複製粘貼到指定目錄下,打開cmd,進入到該目錄下,執行以下命令:

java -jar xxxxxxxx(打成後的jar包名).jar

如:
在這裏插入圖片描述

4、使用Spring的創建嚮導創建Spring boot項目

  依次點擊:File-new-Projects,來到New Projects頁面,然後選中Spring Initializr,選擇JDK,點擊Next:
在這裏插入圖片描述
  來到該頁面,在如下標註項中進行設置,然後點擊Next:
在這裏插入圖片描述
來到如下頁面,在該頁面中選擇需要導入的模塊依賴,然後Next:
在這裏插入圖片描述
然後點擊 確定。等待下載完成後,就創建瞭如下結構的項目:
在這裏插入圖片描述
注意:

1、resources文件夾中目錄結構

  • static:保存所有的靜態資源,如: js css images
  • templates:保存所有的模板頁面(Spring
    Boot默認jar包使用嵌入式的Tomcat,默認不支持JSP頁面);可以使用模板引擎(freemarker、thymeleaf)
  • application.properties:Spring Boot應用的配置文件;可以修改一些默認設置

2、 Controller類上的註解

   @Controller 與@ResponseBody相當於@RestController。將@ResponseBody標註在Controller的類上,表示這個類的所有方法返回的數據直接寫給瀏覽器,如果是對象,可以轉成JSON數據

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