Spring Boot(五)----profile文件路径设置
SpringBoot-profile解析
SpringBoot中使用配置文件application.properties和application.yml两种方式,在这两种方式下分别对应各自的profile配置方式。
一.Properties配置文件环境切换
1、application.properties
首先创建application.properties配置文件,配置文件中需要声明切换的环境名称:
1 | spring.profiles.active=prod |
接下来,需要创建两个profile环境,分别为application-dev.properties和application-prod.properties环境,在其中创建一个对应的person类对象。
application-dev.properties
1 2 | person.name=a person.age=10 |
application-prod.properties
1 2 | person.name=b person.age=21 |
运行结果如下:
- 1.当环境切换至dev开发环境下时:
2.当环境切换至prod环境下时:
二.yaml配置文件环境切换
1、application.yml
在yaml文件中,分为三个document,第一个document为默认的配置文件,第二个document为dev1的配置文件,第三部分为dev2的配置文件,在默认的document中使用spring.profile.active设置使用哪个document的配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | spring: profiles: active: dev1 --- spring: profiles: dev1 person: age: 22 name: zk --- spring: profiles: dev2 person: age: 24 name: zjn |
设置profile为dev2时,启动运行结果如下:
设置profile为dev1时,启动运行结果如下:
项目的目录结构如下:
下面是一些公共文件
ConfigBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.zk.myspringboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigBean { @Autowired Person person; @RequestMapping( "/demo" ) public Person sayHello(){ return person; } } |
MainApp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.zk.myspringboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainApp { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(MainApp. class , args); } } |
Person.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.zk.myspringboot; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "person" ) public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]" ; } } |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <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.zk.myspringboot_006</groupId> <artifactId>myspringBoot_006</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <finalName>myspringboot_006</finalName> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional> true </optional> </dependency> <!-- 继承父包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> </project> |
三、启动设置中切换spring.profiles.active
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | server: port: 8080 spring: profiles: active: prod --- server: port: 8081 spring: profiles: dev --- server: port: 8082 spring: profiles: prod |
运行结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)