上一篇博客中介紹瞭如何建立一個簡單的Spring boot應用,本篇文章介紹Spring boot中的一些基本配置,只有認識和了解這些配置,才能爲咱們之後的深刻學習Spring boot作好鋪墊。php
文章首發於我的博客:【www.xiongfrblog.cn】html
首先,咱們在新建一個Spring boot項目的時候,Spring boot會自動爲咱們在包的根目錄下建立一個名爲xxxApplication.java
的啓動類,該啓動類是咱們項目的入口類,包含一個main
方法,執行該方法就啓動了項目。java
啓動類中有一個核心的註解@SpringBootApplication
,它是一個組合註解,包含如下三個註解:web
@Bean
使用將某個對象註冊到Spring上下文。@Component
,@Service
,@Controller
等標記的類。上邊說過啓動類通常放在包的根目錄下,是由於@ComponentScan註解默認掃描當前包及子包,若是須要指定掃描路徑,須要加上參數,例如
@ComponentScan("com.example.demo.dao")
。spring
Spring boot官方配置了@SpringBootApplication
註解來替代上邊介紹的三個註解,更加簡潔明瞭。apache
Spring boot使用一個全局的配置文件application.properties
或application.yml
,該配置文件通常位於src/main/resource
目錄下,兩種配置文件惟一的區別就在於書寫的格式不同,如今比較主流的是application.yml
格式的配置文件,要熟練使用該種格式的配置文件須要熟悉基礎的yaml
語法,這裏不作過多介紹,本篇博客均使用application.properties
格式的配置文件,下面介紹兩個最基本的配置。瀏覽器
server.port=1188
複製代碼
通常訪問項目的根路徑默認localhost:1188
,可是有時候咱們會在訪問路徑上加上當前項目的名字,這時候就須要修改默認的訪問路徑了。springboot
#spring boot版本2.0如下
server.context-path=/demo
#spring boot版本2.0以上
server.servlet.context-path=/demo
複製代碼
這裏根據Spring boot的版本不一樣有不一樣的配置們你們根據本身的版本選擇對應的屬性名便可,因爲個人Spring boot版本是2.1.2,因此選擇server.servlet.context-path
的屬性名,此時訪問項目的根路徑就的url就變成了localhost:1188/demo
。bash
固然,Spring boot中能夠配置屬性還有不少,好比配置 Email:app
spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
spring.mail.host= # SMTP server host. For instance, `smtp.example.com`.
spring.mail.jndi-name= # Session JNDI name. When set, takes precedence over other Session settings.
spring.mail.password= # Login password of the SMTP server.
spring.mail.port= # SMTP server port.
spring.mail.properties.*= # Additional JavaMail Session properties.
spring.mail.protocol=smtp # Protocol used by the SMTP server.
spring.mail.test-connection=false # Whether to test that the mail server is available on startup.
spring.mail.username= # Login user of the SMTP server.
複製代碼
這裏就不列舉了,太多了,具體能夠查閱官方文檔【傳送門】
在實際項目中,不少狀況咱們須要定義一些全局屬性,在須要的地方注入使用便可,Spring boot容許咱們在application.properties
下自定義一些屬性,下面介紹自定義屬性而且使用。
blog.login.name=admin
blog.login.pass=1234
複製代碼
咱們在application.properties
定義了blog.login.name
和blog.login.pass
兩個屬性。
爲了方便演示,咱們定義一個名爲TestController.java
的控制器,使用@Value
註解注入屬性,所有代碼以下:
package com.web.springbootconfig.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/** * @author Promise * @createTime 2018年12月27日 下午10:11:35 * @description */
@Controller
public class TestController {
@Value("${blog.login.name}")
private String name;
@Value("${blog.login.pass}")
private String pass;
@RequestMapping("/test")
@ResponseBody
public String configTest() {
return "name="+name+";pass="+pass+";nick="+nick;
}
}
複製代碼
能夠看到使用@Value(屬性名名)
的方式注入咱們自定義的屬性,此時啓動項目瀏覽器訪問localhost:1188/demo/test
或127.0.0.1:1188/demo/test
獲得以下結果:
由圖能夠看出,咱們拿到了在配置文件中自定義屬性的值admin
和1234
。
這裏有一點須要你們特別注意,只有在被Spring管理的類中才能注入屬性,好比上文中咱們的
TestController.java
控制器類名上加了@Controller
註解,代表將此類交由Spring管理。
新建一個Text.java
類,內容以下:
package com.web.springbootconfig.entity;
import org.springframework.beans.factory.annotation.Value;
/** * @author Promise * @createTime 2019年1月14日 下午11:35:52 * @description */
public class Test {
@Value("${blog.login.name}")
private String name;
@Value("${blog.login.pass}")
private String pass;
//省略getter,setter方法
}
複製代碼
再在TestController.java
控制器中添加以下方法:
@RequestMapping("/check")
@ResponseBody
public String check() {
Test test =new Test();
return "name="+test.getName()+";pass="+test.getPass();
}
複製代碼
重啓項目,瀏覽器訪問localhost:1188/demo/check
,此時獲得以下結果:
能夠看到輸出爲null
,正是由於咱們的Text.java
類沒有交由Spring管理,因此Spring並無爲咱們自動注入屬性,這點必定要注意!
從以前的操做中,不少小夥伴確定已經發現了咱們每次對項目作修改的時候都須要手動重啓項目纔可以正確訪問到新的內容,這對咱們日常的開發來講確定很是不友好的,別擔憂,Spring boot也爲咱們設計了熱部署功能,修改完代碼保存以後Spring boot會自動重啓加載最新的內容,只須要咱們簡單的配置便可。
Spring爲開發者提供了一個名爲spring-boot-devtools的模塊來使Spring boot應用支持熱部署,提升開發者的開發效率,無需手動重啓Spring boot應用。
在pom.xml
文件中添加Devtools
依賴,代碼以下:
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
複製代碼
而且修改build
模塊,以下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
複製代碼
在配置文件中添加以下內容(其實不添加也能夠,由於默認就是打開的,這裏提出來是方便有時候能夠關閉熱部署。)
spring.devtools.restart.enabled: true
複製代碼
須要關閉熱部署功能時將屬性值設置爲false
便可。
好了,至此熱部署的功能就已經配置完成了,如今能夠啓動項目,而後隨便修改一個文件保存,你會發現控制檯已經更新了日誌,等待項目從新啓動以後發現更新的內容已經展現在瀏覽器上了。
在實際的企業級開發過程當中,咱們老是有開發環境,生產環境等不一樣的環境,同一個項目在每個不一樣的環境須要的配置老是不同的,這時候若是每換一個環境就修改一次application.properties
配置文件的內容,對開發人員來講是很是糟糕的體驗,因此Spring boot爲咱們提供了不一樣環境指定特定的配置文件的功能,而咱們只須要簡單的配置便可,完美解決難題。
多環境配置文件必須以application-{profile}.properties的格式命名,其中{profile}爲環境標識,好比咱們有一個開發環境需設置啓動端口爲1188,一個生產環境需設置啓動端口爲1189,兩個配置文件分別爲:
server.port=1188
複製代碼
server.port=1189
複製代碼
啓動項目具體加載哪一個配置文件須要在application.properties
配置文件中添加以下代碼指定(好比咱們使用開發環境):
spring.profiles.active=dev
複製代碼
Spring boot能夠將項目打成不一樣類型的包,好比jar包,war包,pom包等,這裏咱們只介紹最長用的jar包,打包須要在pom.xml文件中有以下的配置(這裏貼出完整的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.web</groupId>
<artifactId>springboot-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
複製代碼
其中關於打包的重要代碼爲:
指定打包的類型
<packaging>jar</packaging>
複製代碼
Spring Boot Maven打包插件:spring-boot-maven-plugin
,該插件將項目打成一個可執行的jar包,包括把應用程序的全部依賴打入jar文件內,能讓你在命令行用java -jar來運行應用程序。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
複製代碼
右鍵項目選擇Run As
->Maven build
,在彈出框的Goals
輸入clean package
點擊Run
便可。
打包前在項目的根目錄下會發現一個空的target
文件夾,打包生成的文件會放在這個文件夾內,打包前:
打包後:
能夠看到生成了一個jar文件。
命令行進入jar包所在的目錄,運行Java -jar jar包名.jar
便可啓動項目,此時啓動瀏覽器訪問localhost:1188/demo/test
能夠看到與以前同樣的內容。
在執行jar包的時候咱們也能夠指定參數,好比指定端口號爲8089java -jar jar包名.jar --server.port=8089
,也能夠指定使用哪一個環境的配置文件等不少東西,這裏不一一律述了。
好了,Spring boot的一些基礎的配置就說到這裏,更多的內容須要咱們本身在不斷的實踐中繼續瞭解,下期內容再見,bye~