Spring Boot(二) 配置文件

文章導航-readme

1、配置Spring Boot熱部署

    技術的發展老是由於人們想偷懶的心理,若是咱們不想每次修改了代碼,都必須重啓一下服務器,並從新運行代碼。那麼能夠配置一下熱部署。有了它以後,修改了代碼只須要從新build一下,就能夠看到效果了,不須要重啓服務器。html

1.配置熱部署

  1. pom.xml文件中添加以下依賴:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>                                        <optional>true</optional>
        </dependency>
  1. 修改pom.xml文件
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--開啓熱部署-->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

修改pom.xml文件後,idea會彈出一個讓你自動導入包的文件,點擊Import Changes。就會自動將jar包下載到依賴庫中。java

如此,就實現了Spring Boot的熱部署,此時修改咱們的代碼,只需從新Build一下就能夠了。git

2. 配置自動build

固然,若是你更懶的話,練build都不想作,也能夠在Idea中配置自動Build(自動Build僅支持Spring Boot項目)。github

  1. 打開Idea->File->Settings...能夠看到以下界面

選中上圖中的Build Project automaticalweb

  1. 按組合鍵Shift+ALT+Ctrl+/選擇Registry能夠看到以下界面

選中上圖中的complier.automake.allow.when.app.runningspring

如此,修改咱們的代碼後,無需從新build也無需從新重啓。服務器

2、Spring Boot讀取配置文件

  1. 修改咱們的配置文件application.properties
server.port=8888

<!--網站配置-->
website.name=Loading
website.domin=www.loading.ink
website.title=個人博客網站
website.description=分享個人生活和技術
  1. 新建配置文件類WebSiteConfig
package spring.boot.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.text.MessageFormat;

@Configuration
//@ConfigurationProperties(prefix = "website")
//要讀取的配置文件地址
@PropertySource(value = "classpath:application.properties")
public class WebSiteConfig {
    @Value("${website.title}")
    private String title;
    @Value("${website.domain}")
    private String domain;
    @Value("${website.description}")
    private String description;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return MessageFormat.format("Title:{0} Domin:{1} Description:{2}", title, domain, description);
    }
}

讀取配置文件中的配置有兩種方式app

1.@ConfigurationProperties(prefix = "website")用於綁定屬性,其中prefix表示所綁定的屬性的前綴。若是配置文件中的配置和屬性名一致能夠用此種方式dom

2.@Value("${website.title}") 綁定配置文件中的屬性maven

注意:

註解@Configuration用於定義配置類

註解@PropertySource(value = "classpath:application.properties")表明要讀取配置文件的路徑當配置文件是application.properties時,這個註解能夠省略不寫

  1. 新建WebSiteController
package spring.boot.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import spring.boot.web.config.WebSiteConfig;

@RequestMapping("/website")
@RestController
public class WebSiteController {

    //經過@Autowired註解注入bean
    @Autowired
    private WebSiteConfig webSiteConfig;

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        return webSiteConfig.toString();
    }
}
  1. 測試運行

  1. 注意,若是第一次運行時遇到讀取配置文件裏的中文亂碼的問題。其主要緣由多是由於配置文件的格式並非utf-8的格式。此時可在idea中進行設置。

如上圖所示更改成uft-8,注意後面的必定要勾選上。

經過上面配置後若是還不行,能夠將配置文件刪除後從新建一個,問題就能夠解決!

3、Spring Boot Profile

    Spring Boot 使用一個全局的配置文件 application.properties ,Spring Boot 的全局配置文件的做用是對一些默認配置的配置值進行修改。

    在平常開發中,咱們經常會遇到一個問題。就是在不一樣的環境使用不一樣的配置。好比生產、開發、測試三個不一樣的環境,咱們的配置確定不同。這時,咱們就要用到Profile。

    Profile 是 Spring 用來針對不一樣的環境對不一樣的配置提供支持的,全局 Profile 配置使用 application-{profile}.properties(如 application-dev.properties)。經過在 application.properties 中設置 spring.profiles.active = dev 來指定活動的 Profile

  1. 依次再目錄下面新建三個配置文件,application-dev.propertiesapplication-test.propertiesapplication-prod.properties。它們分別表明開發環境、測試環境、生產環境的配置文件。
server.port=8887

website.title=個人博客網站--Dev
website.domain=www.loading.ink
website.description=分享個人技術與生活
server.port=8886

website.title=個人博客網站--test
website.domain=www.loading.ink
website.description=分享個人技術與生活
server.port=8885

website.title=個人博客網站--prod
website.domain=www.loading.ink
website.description=分享個人技術與生活
  1. 接下來修改application.properties:表示,將採用application-dev.properties這個配置文件。
spring.profiles.active=dev

測試運行咱們能夠看到會啓動開發環境配置文件的端口8887

注意:配置文件會優先獲取Profile中的配置,若是Profile中沒有的配置項, 那麼會直接取application.properties中的配置

示例代碼

相關文章
相關標籤/搜索