多环境配置demo代码:点击查看LearnSpringBoot02
点击查看更多的SpringBoot教程
方式一、多个properties文件配置
注意:创建properties文件,命名规则:application-(环境名称)
示例:application-dev.properties
在resource里创建application-dev.properties文件
在resource里创建application-product.properties文件
application.properties文件里切换环境配置
1、启动dev环境输出的日志
在浏览器访问http://localhost:8061/hello效果图
2、启动prdouct环境输出的日志
在浏览器访问http://localhost:8090/hello效果图
方式二、yml文件配置方式
在resource里创建application.yml文件
注意:如果使用这里配置端口,运行项目时需要将 application.properties 文件里的端口配置注释掉
1、启动dev环境输出的日志
在浏览器访问http://localhost:8991/hello效果图
2、启动prdouct环境输出的日志
在浏览器访问http://localhost:8992/hello效果图
pom.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example.springconfig</groupId><artifactId>LearnSpringBoot02</artifactId><version>0.0.1-SNAPSHOT</version><name>LearnSpringBoot02</name><description>Demo project for Spring Boot Config</description><properties><java.version>17</java.version></properties><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><!-- JSR303数据校验--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><!--https://docs.spring.io/spring-boot/docs/3.1.1/reference/html/configuration-metadata.html#appendix.configuration-metadata.annotation-processor
--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml代码
server:port: 8990
spring:profiles:active: product
---
server:port: 8991
spring:config:activate:on-profile: dev
---
server:port: 8992
spring:config:activate:on-profile: product# 注意:如果使用这里配置端口,需要将 application.properties 文件里的端口配置注释掉#person:
# lastName: Test
# age: 20
# boss: false
# birth: 2018/12/12
# maps: {k1: v1, k2: 12}
# lists:
# - java
# - android
# - ios
# - python
#
# dog:
# name: dahuang
# age: 6
HelloWorldControl.java代码
package com.example.springconfig.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloWorldControl {@RequestMapping("/hello")public String hello(){return "hello world RestController";}//这里测试业务逻辑需要获取配置文件中的某个值 使用 @Value 读取配置文件值@Value("${person.lastName}")private String name;@RequestMapping("/sayhello")public String sayHello(){return "hello "+name;}
}