springboot2.x簡單詳細教程--高階篇幅之多環境配置(第十四章)
阿新 • • 發佈:2018-11-24
一、SpringBoot多環境配置介紹和專案實戰(核心知識)
簡介:SpringBoot介紹多環境配置和使用場景
1、不同環境使用不同配置
例如資料庫配置,在開發的時候,我們一般用開發資料庫,而在生產環境的時候,我們是用正式的資料
2、配置檔案存放路徑
classpath根目錄的“/config”包下
classpath的根目錄下
3、spring boot允許通過命名約定按照一定的格式(application-{profile}.properties)來定義多個配置檔案
1)三個配置
2)開發環境的配置
3)測試環境的配置
4)pom中就是基本的配置
<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> <groupId>com.itcast</groupId> <artifactId>Springboot08_A</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5)主程式
package com.itcast.demo; 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); } }
6)controller
package com.itcast.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itcast.demo.domain.JsonData;
/**
*@作者po
*/
@RestController
@RequestMapping("/api/v1")
public class OrderController {
@Value("${test.url}")
private String domain;
/**
* 功能描述:多環境配置
* @param msg
* @return
*/
@GetMapping("lanpo")
public Object order(String msg){
return JsonData.buildSuccess(domain);
}
}
7)