Spring Boot(五)----profile文件路径设置
SpringBoot-profile解析
SpringBoot中使用配置文件application.properties和application.yml两种方式,在这两种方式下分别对应各自的profile配置方式。
一.Properties配置文件环境切换

1、application.properties
首先创建application.properties配置文件,配置文件中需要声明切换的环境名称:
spring.profiles.active=prod
接下来,需要创建两个profile环境,分别为application-dev.properties和application-prod.properties环境,在其中创建一个对应的person类对象。
application-dev.properties
person.name=a person.age=10
application-prod.properties
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的配置。
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
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
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
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
<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
server:
port: 8080
spring:
profiles:
active: prod
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: prod
运行结果

浙公网安备 33010602011771号