application配置和profile隔離配置


前言

  github: https://github.com/vergilyn/SpringBootDemo

  說明:我代碼的結構是用profile來區分/激活要加載的配置,從而在一個project中寫各種spring boot的demo。所以很多時候可能在*Application.class中指定了特殊的profile。

    這種方式可能很糟糕(在細看理解application.properties的加載順序后,感覺在*Application.class目錄下寫application.properties應該更好。)

  代碼位置:

  image

一、application.properties

  1. 1 application.properties的加載順序 (重要

    SpringApplication將從以下位置加載application.properties文件,並把它們添加到Spring Environment中:
      (1) 當前目錄下的一個/config子目錄;
      (2) 當前目錄;
      (3) 一個classpath下的/config包;
      (4) classpath根路徑(root);
    這個列表是按優先級排序的(列表中位置高的將覆蓋位置低的)。

  1.2 application.properties 加載目錄/文件名 修改

    如果不喜歡將application.properties作為配置文件名,可以通過指定spring.config.name環境屬性來切換其他的名稱。

    也可以使用spring.config.location環境屬性來引用一個明確的路徑(目錄位置或文件路徑列表以逗號分割)。(並不是在config/application.properties中指定)

image

 

二、spring boot的profile指定

  2.1 什么是profile,怎么理解profile?

    Spring Profiles提供了一種隔離應用程序配置的方式,並讓這些配置只能在特定的環境下生效。

   任何@Component或@Configuration都能被@Profile標記,從而限制加載它的時機

    比如:開發環境與正式環境的數據庫連接配置文件不一樣,在開發服務器指定為開發配置文件,在正式環境時切換正式的配置文件。(多看代碼去理解)

  2.2 profile的加載順序、格式

    格式:application-{profile}.properties

    加載順序:與application.properties一樣。

    profile中的配置會覆蓋默認的配置,包括application.properties中的配置。

  2.3 profile在spring boot中的配置

    (1) application.properties中指定屬性配置

#### 特定配置profile,多個可用逗號(,),ex:spring.profiles.active=dev,prod
## 特定Profile屬性從跟標准application.properties相同的路徑加載,並且特定profile文件會覆蓋默認的配置。
spring.profiles.active=dev
## 用來無條件的添加生效的配置。或 SpringApplication.setAdditionalProfiles(String... profiles)
## 可能在pom中依賴了部分jar,所以可能必須使用手動配置(否則spring boot會用jar中的自動配置,比如database)
spring.profiles.include=log,thymeleaf,db

    (問題: active與include有什么區別?)

   (2) 用SpringApplication中的API配置

image

  (3) 代碼中限制加載及demo

復制代碼
@Controller
//@Profile注解可以實現不同環境下配置參數的切換,任何@Component或@Configuration注解的類都可以使用@Profile注解。
@Profile("dev")
public class DevController {
	@Value("${app.name}")
	private String appName;
	@Value("${app.description}")
	private String appDesc;

	@RequestMapping("/profile")
	public String greeting(
			@RequestParam(value = "name", required = false, defaultValue = "VergiLyn") String name,
			Model model) {
		model.addAttribute("appDesc", appDesc);
		model.addAttribute("name", appName);
		return "greeting";
	}
}
復制代碼
復制代碼
@Controller
@Profile("prod")
public class ProdController {
	@Value("${app.name}")
	private String appName;
	@Value("${app.description}")
	private String appDesc;

	@RequestMapping("/profile")
	public String greeting(
			@RequestParam(value = "name", required = false, defaultValue = "VergiLyn") String name,
			Model model) {
		model.addAttribute("appDesc", appDesc);
		model.addAttribute("name", appName);
		return "greeting";
	}
}
復制代碼
復制代碼
@SpringBootApplication
public class ProfileApplication {
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(ProfileApplication.class);
		app.setAdditionalProfiles("dev");	// dev 或prod
		app.run(args);
	}

}
復制代碼

application-dev.properties

## 屬性占位符
app.name=dev_spring_boot

application-prod.properties

## 屬性占位符
app.name=prod_spring_boot

application.properties

復制代碼
 
app.name=default_spring_boot
app.description=${app.name} is a Spring Boot application
## thymeleaf 配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false
## dev/prod,此demo在java代碼中指定
# spring.profiles.active=dev
# spring.profiles.include=...
復制代碼

greeting.html

復制代碼
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
復制代碼

結果:

   如果profile啟用”dev”,則app.name=dev_spring_boot,訪問localhost:8080/profile被加載注入的bean是DevController。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM