spring boot開發總結一(基礎)

 

目錄

1.簡介

2.手動搭建spring boot項目

3.idea搭建spring boot項目

4. Eclipse搭建spring boot項目

 5.控制器的編寫

6. spring boot的相關配置

8. spring boot打包

(1)打jar包

(2)打war包

9. 功能應用篇

10. 其他應用篇


1.簡介

        它使用“習慣優於配置”(項目中存在大量的配置,此外還內置一個習慣性的配置,讓你無須)的理念讓你的項目快速運行起來。它並不是什麼新的框架,而是默認配置了很多框架的使用方式,就像Maven整合了所有的jar包一樣,Spring Boot整合了所有框架


2.手動搭建spring boot項目

(1)新建maven項目

(2)添加依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

(3)編寫啓動類

package com.demo.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
        @SpringBootApplication
    public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3.idea搭建spring boot項目

(1)新建項目,選擇Spring Initializr,然後選擇默認的url點擊【下一個】

(2)然後修改一下項目的信息

(3)勾選上Web模板

(4)選擇好項目的位置,點擊【完成】

4. Eclipse搭建spring boot項目

(1)下載安裝Eclipse的STS插件

(2)選擇新建項目 - 春 - 春啓動項目,新建春啓項目。


 5.控制器的編寫

(1)新建類,類上添加註解

@RestController //@Controller和@ResponseBody註解的合體版
@RequestMapping("xxx") // 指明該類訪問路徑,可不寫

(2)新建方法,方法上加註解

@RequestMapping(value="yyy") // 指明該ff訪問路徑

(3)啓動啓動類可啓動項目(main方法)

(4)訪問路徑爲:/ xxx / yyy

6. spring boot的相關配置

(1)簡介

Spring Boot使用一個全局的配置文件application.properties或application.yml,放置在【src / main / resources】目錄或者類路徑的/配置。

Spring Boot不僅支持常規的屬性配置文件,還支持yaml語言的配置文件.yaml是以數據爲中心的語言,在配置數據的時候具有面向對象的特徵。

(2)默認配置的配置值進行修改

#配置程序端口,默認爲8080
server.port=40000
#session過期時間,以秒爲單位
server.session.timeout=1800
#配置默認訪問路徑,默認爲/
server.context-path=/
#配置Tomcat編碼,默認爲UTF-8
server.tomcat.uri-encoding=UTF-8
#配置最大線程數 默認爲200
server.tomcat.max-threads=1000

(3)配置信息獲取

        定義變量,上面加註解@Value( “$ {XXXX}”),配置文件在項目啓動時加載

        封裝配置信息,新建實體類,類上加註解

@Component
//prefix 表示獲取前綴爲xxx的配置信息,添加set/get方法  locations  表示配置文件路徑,src/main/resources目錄下
@ConfigurationProperties(locations = "classpath:mail.properties",prefix = "xxx")

        Environment對象獲取

//引入Environment 變量
@Resource
private Environment env;
//在方法中獲取配置信息
env.getProperty("name")

字段爲配置key的剩餘部分,如:student.name - > student爲前綴,name爲實體類字段

(6) 使用隨機數

      在spring boot的屬性配置文件中,可以通過使用${random}配置來產生隨機的int值、long值或string字符串

(7) 多環境配置

      在spring boot中,多環境配置的文件名需要滿足 application-{profile}.properties的格式,其中{profile}對應你的環境標識。

application-dev.properties:開發環境

    在application.properties中配置通用內容,並設置spring.profiles.active=dev,以開發環境爲默認,在application-{profile}.properties中配置各個環境不同的內容,通過命令行方式去激活不同環境的配置。

 

7. spring boot打包

(1)打jar包

        1)pom文件配置

    <packaging>jar</packaging>
    <build>
        <finalName>bg_anaytics</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

         2)執行maven的package命令,生成jar包到 target目錄下

         3)執行 java -jar xxxxx.jar 啓動項目

(2)打war包

        1)pom文件配置

    <packaging>war</packaging>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

         2)添加ServletInitializer

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
//由於我們採用web3.0 規範,是沒有web.xml 的,而此類的作用與web.xml相同。
public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

         3)執行maven的package命令,生成war包到 target目錄下

         4)將war 包放入tomcat,啓動tomcat

 

 

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