SpringBoot2.X快速構建和配置

1、SpringBoot是什麼?

SpringBoot用於簡化Spring應用配置,採用“約定優於配置”的方式開發,可以快速構建Spring應用。

2、準備工作

在這裏插入圖片描述

3,HelloWord環境搭建

3.1官方向導搭建boot應用

  1. 地址:http://start.spring.io/

  2. 設置項目屬性:
    在這裏插入圖片描述

  3. 解壓,拷貝到工作空間,導入maven項目

  4. 寫Controller: HelloController.java
    在這裏插入圖片描述
    注意: 這個controller也可以不寫就可以直接啓動應用,寫一定要寫在app的同一級目錄,或者子目錄,後面再說寫在其他地方的辦法

  5. 啓動Spring Boot入口類:App
    在這裏插入圖片描述
    @SpringBootApplication表示SpringBoot應用

3.2普通maven工程搭建boot應用

  1. 新建一個普通的maven工程,選擇quickstart
    【注意:Spring boot是web工程,但是我們這裏只需要建立quickstart即可,因爲spring boot內嵌了servlert容器】

或者不用這個模板也是可以,只是後面自己手工添加相關代碼就可以了
在這裏插入圖片描述

. 拷貝依賴的父pom到自己的工程pom文件中:
所有Spring Boot組件的基礎引用

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

提供web的支持

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

maven 打包插件

<plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

添加編譯環境,一般都有

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

Run as --> Java Application啓動SampleController.java
瀏覽器輸入:http://localhost:8080/ 即可

完整的pom文件

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

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>com.hrp.helloworld</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>helloworld</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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

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

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

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

當然,除了以上兩種方式搭建boot工程,也可以通過其它工具快速生成,例如idea , sts,spring boot cli等
這些工具集成了spring boot特性 ,可以一鍵生成springboot工程骨架

4. SpringBoot的目錄結構

在這裏插入圖片描述

5.springboot測試類

  1. 添加測試支持依賴:spring-boot-starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

注意:加入這個依賴之後,junit包就可以不用了,因爲test的starter中包含了junit

2.在測試包中建立測試程序類,測試App

@SpringBootTest    //spring boot test 支持
@WebAppConfiguration  //按照web方式進行啓動 
@RunWith(SpringJUnit4ClassRunner.class) //使用spring junit測試
public class AppTest 
{
   @Autowired
   private HelloController helloController;

    @Test
    public void shouldAnswerWithTrue()
    {
        TestCase.assertEquals(helloController.hello(),"Hello spring boot");
    }
}

6.SpringBoot常用配置

在這裏插入圖片描述

6.1修改端口

application.properties:
server.port=8888
另外,也可以直接在運行jar包的時候修改
java -jar xx.jar --server.port=8888

6.2 自定義屬性及獲取

@Value("${屬性名}")獲取對應的屬性值
在這裏插入圖片描述
在這裏插入圖片描述
不過一個有趣的事情是:username獲取的不是自己配置的信息
在這裏插入圖片描述
難得username這個變量是特殊變量? 先不深究

6.3參數引用

application.properties
teacher.id=1
teacher.name=zhangsan
teacher.info=Teacher ${teacher.name}'s number is ${teacher.id}

6.4 隨機內容生成

隨機字符串

random.string=${random.value}

隨機int

random.number=${random.int}

隨機long

random.long=${random.long}

1-20的隨機數

random.b=${random.int[1,20]}

6.5 多環境配置

我們在開發應用時,通常一個項目會被部署到不同的環境中,比如:開發、測試、生產等。其中每個環境的數據庫地址、服務器端口等等配置都會不同,對於多環境的配置,大部分構建工具或是框架解決的基本思路是一致的,通過配置多份不同環境的配置文件,再通過打包命令指定需要打包的內容之後進行區分打包,Spring Boot也提供了支持
在Spring Boot中多環境配置文件名需要滿足application-{profile}.properties的格式,其中{profile}對應你的環境標識,比如:
 application-dev.properties:開發環境
 application-test.properties:測試環境
 application-prod.properties:生產環境
至於哪個具體的配置文件會被加載,需要在application.properties文件中通過spring.profiles.active屬性來設置,其值對應{profile}值。
比如:spring.profiles.active=dev就會加載application-dev.properties配置文件中的內容
案例:
在這裏插入圖片描述
在dev, test, prod這三個文件均都設置不同的server.port端口屬性,如:dev環境設置爲8081,test環境設置爲8082,prod環境設置爲8083
application.properties中設置spring.profiles.active=dev,就是說默認以dev環境設置
總結:
1.application.properties中配置通用內容,並設置spring.profiles.active=dev,以開發環境爲默認配置
2.application-{profile}.properties中配置各個環境不同的內容

其他的spring的全部配置參考配置文檔

6.6 配置文件還有一種方式yml

yml配置
SpringBoot支持兩種格式的配置文件
屬性文件:application.properties
Yml:application.yml
yml是一種簡介的非標記語言。yml以數據爲中心,使用空白,縮進,分行組織數據,從而使得表示更加簡潔易讀。
yml語法格式:
標準格式:key:(空格)value
使用空格代表層級關係,以“:”結束
yml和properties同時都存在時,以properties爲主。

7 Banner

Banner是指SpringBoot啓動時顯示的字符畫,默認是“spring”。我們可以新建resources/banner.txt進行修改。
關閉banner:

8 Spring boot 日誌

ava 有很多日誌系統,例如,Java Util Logging, Log4J, Log4J2, Logback 等。Spring Boot 也提供了不同的選項,比如日誌框架可以用 logback 或 log4j ,log4j2等。
在這裏插入圖片描述
常用配置項
日誌常用配置項 默認值 備註
logging.file 日誌輸出的文件地址
logging.level.ROOT info 設置日誌的輸出級別
logging.level.* info 定義指定包的輸出級別
logging.config logback-spring.xml 日誌的配置文件
注意:SpringBoot默認並沒有進行文件輸出,只在控制檯中進行了打印。
日誌級別:debug>info>warn>error,默認情況下springboot日誌級別爲info
如果設置了debug=true時,日誌會降級爲debug

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