一、SpringBoot多环境配置介绍和项目实战(核心知识)
简介:SpringBoot介绍多环境配置和使用场景
1、不同环境使用不同配置
例如数据库配置,在开发的时候,我们一般用开发数据库,而在生产环境的时候,我们是用正式的数据
![](https://img-blog.csdnimg.cn/20181110171842105.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYW5jaGlsdQ==,size_16,color_FFFFFF,t_70)
2、配置文件存放路径
classpath根目录的“/config”包下
classpath的根目录下
3、spring boot允许通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件
1)三个配置
2)开发环境的配置
3)测试环境的配置
4)pom中就是基本的配置
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.itcast</groupId> 5 <artifactId>Springboot08_A</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>2.0.1.RELEASE</version> 11 </parent> 12 <dependencies> 13 <dependency> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-web</artifactId> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-test</artifactId> 20 <scope>test</scope> 21 </dependency> 22 23 </dependencies> 24 <build> 25 <plugins> 26 <plugin> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-maven-plugin</artifactId> 29 </plugin> 30 </plugins> 31 </build> 32 33 </project>
5)主程序
1 package com.itcast.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 8 public class Application { 9 10 public static void main(String[] args) { 11 SpringApplication.run(Application.class, args); 12 } 13 14 }
6)controller
1 package com.itcast.demo.controller; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import com.itcast.demo.domain.JsonData; 9 10 /** 11 *@作者po 12 */ 13 @RestController 14 @RequestMapping("/api/v1") 15 public class OrderController { 16 17 @Value("${test.url}") 18 private String domain; 19 20 /** 21 * 功能描述:多环境配置 22 * @param msg 23 * @return 24 */ 25 @GetMapping("lanpo") 26 public Object order(String msg){ 27 28 return JsonData.buildSuccess(domain); 29 } 30 }
7)
8)改成开发环境
9)测试环境