1 多環境配置檔案
在實際開發的過程中,我們的項目會經曆很多的階段(開發->測試->上線),每個階段的配置也會不同,例如:端口、上下文根、資料庫等,那麼這個時候為了友善在不同的環境之間切換,SpringBoot 提供了多環境配置,具體步驟如下: 目錄結構
dev
#開發環境配置檔案
server.port=8080
server.servlet.context-path=/dev
product
#生産環境配置檔案
server.port=8083
server.servlet.context-path=/product
ready
#準生産環境配置檔案
server.port=8082
server.servlet.context-path=/ready
test
#測試環境配置檔案
server.port=8081
server.servlet.context-path=/test
核心配置檔案
#springboot核心配置檔案
spring.profiles.active=test
控制器類
import com.abc.springboot.web.SpringBootController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
*/
@Controller
public class IndexController {
@RequestMapping(value = "/say")
@ResponseBody
public String say() {
return "Hello springboot multi-environments";
}
}
入口類
import com.abc.springboot
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
結果截圖
spring.profiles.active=dev
spring.profiles.active=test
2.自定義配置檔案
在 SpringBoot 的核心配置檔案中,除了使用内置的配置項之外,我們還可以在自定義配置,然後采用如下注解去讀取配置的屬性值
2.1 @Value
核心配置檔案
在核心配置檔案 applicatin.properties 中,添加兩個自定義配置項 school.name 和
website。在 IDEA 中可以看到這兩個屬性不能被 SpringBoot 識别,背景是桔色的
在核心配置檔案 applicatin.yml 中,添加兩個自定義配置項 school.name 和website。
server:
port: 9090
servlet:
context-path: /
school:
name: ssm
websit: http://www.baidu.com
測試代碼
package com.liuhaiyang.springboot.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;
@Controller
public class OneController {
@Value("${schools.name}")
public String schoolName;
@Value("${websit}")
private String websit;
@RequestMapping("one")
@ResponseBody
public String onetest(){
return "hello"+schoolName+": "+websit;
}
}
2.2 @ConfigurationProperties
自定義配置檔案
#設定内嵌Tomcat端口号
server.port=8080
#設定上下文根
server.servlet.context-path=/
first.name=tencent
first.website=https://www.tencent.com
second.name=baidu
second.website=https://www.baidu.com
自定義一個類,擷取自定義配置檔案中的屬性值
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
*
*/
@Component
@ConfigurationProperties(prefix = "first") //@ConfigurationProperties(prefix = "first") 這個注解,相當于擷取到屬性的字首名,first.name、first.website。
public class First {
private String name;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
控制器類
import com.songzihao.springboot.config.First;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
*/
@Controller
public class IndexController {
@Autowired
private First first;
@RequestMapping(value = "/say")
@ResponseBody
public String say() {
return "first.name===" + first.getName() + " , first.website===" + first.getWebsite();
}
}
入口類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
結果截圖