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>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM