1. 程式人生 > >spring boot中多環境配置支援

spring boot中多環境配置支援

一、說明

    在我們的日常開發中,生產環境的配置和測試環境的配置以及開發環境的配置基本上都是不相同的,每次到部署環境的時候,就需要手動的切換配置檔案,如果在切換的過程中一不小心的話,就會出錯,所以在開發中,一般會搞個配置檔案檢查的功能,來避免出錯,而spring boot則充分考慮了這種情況,為開發人員提供了天然的多環境配置支援。

二、增加properties配置檔案

1、application-dev.properties

# 開發環境的部署埠
server.port=7980
# 自定義屬性
com.chhliu.springboot.author=chhliu
# 引數間引用
com.chhliu.springboot.age=${com.chhliu.age}
com.chhliu.springboot.sex=man
com.chhliu.springboot.time=20170123
com.chhliu.age=28
2、application-prod.properties
server.port=7982
com.chhliu.springboot.author=xyh
com.chhliu.springboot.age=${com.chhliu.age}
com.chhliu.springboot.sex=woman
com.chhliu.springboot.time=20170123
com.chhliu.age=27
3、application-test.properties
server.port=7984
com.chhliu.springboot.author=chhliuxyh
com.chhliu.springboot.age=${com.chhliu.age}
com.chhliu.springboot.sex=woman
com.chhliu.springboot.time=20170123
com.chhliu.age=24
4、application-BEIJING.properties
server.port=7981
只要配置檔案的命名滿足application-{profile}.properties格式就行

三、指定具體使用哪個配置

application.properties

spring.profiles.active=prod // 指定具體使用哪種配置環境,此處指定使用application-prod.properties配置檔案中的環境
四、新建自定義屬性對應的實體類
package com.chhliu.springboot.properties.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author chhliu
 * 自定義屬性對應的實體類
 * spring boot會將配置檔案中自定義的屬性值,自動設定到該類對應的屬性上,使用的使用直接注入該類即可
 * prefix用來指定自定義屬性值的字首
 */
@ConfigurationProperties(prefix="com.chhliu.springboot")
public class ConfigProperties {
	private String author;
	private int age;
	private String sex;
	private String time;
	
        ……省略getter,setter方法……
	@Override
	public String toString() {
		return "ConfigProperties [author=" + author + ", age=" + age + ", sex=" + sex + ", time=" + time + "]";
	}
}
五、新建一個業務介面
package com.chhliu.springboot.properties.service;

public interface PropertiesEnv {
	String getPropertiesEnv();
}
六、介面實現類

1、實現類一

package com.chhliu.springboot.properties.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import com.chhliu.springboot.properties.config.ConfigProperties;

/**
 * @author chhliu
 * 其中@Profile用來指定工作環境,例如示例中為“dev”,那麼該類只會在配置檔案為“dev”的環境下,才會呼叫該類
 */
@Service("devPropertiesEnv")
@Profile("dev") // 當配置為開發環境時,生效
public class DevPropertiesEnv implements PropertiesEnv{
	@Autowired // 注入自定義屬性對應的實體類
	private ConfigProperties properties;
	@Override
	public String getPropertiesEnv() {
		return properties.toString();
	}
}
2、實現類二
package com.chhliu.springboot.properties.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import com.chhliu.springboot.properties.config.ConfigProperties;

/**
 * @author chhliu
 * 其中@Profile用來指定工作環境,例如示例中為“prod”,那麼該類只會在配置檔案為“prod”的環境下,才會呼叫該類
 */
@Service("proPropertiesEnv")
@Profile("prod")// 當配置為生產環境時,生效
public class ProPropertiesEnv implements PropertiesEnv {

	@Autowired
	private ConfigProperties properties;
	@Override
	public String getPropertiesEnv() {
		return properties.toString();
	}
}
七、開啟支援
package com.chhliu.springboot.properties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import com.chhliu.springboot.properties.config.ConfigProperties;

@SpringBootApplication
@EnableConfigurationProperties({ConfigProperties.class}) // 開啟配置屬性支援
public class SpringbootPropertiesApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootPropertiesApplication.class, args);
	}
}
八、controller編寫
package com.chhliu.springboot.properties.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chhliu.springboot.properties.service.PropertiesEnv;

@RestController
public class HelloWorld {
	
	@Autowired // 注入介面服務
	private PropertiesEnv env;
	
	@GetMapping("/hello")
	public String sayHello(){
		return "world";
	}
	
	@GetMapping("properties")
	public String getPropertiesEnv(){
		return env.getPropertiesEnv();
	}
}
九、測試

Tomcat輸出

Tomcat started on port(s): 7982 (http)
發現,此時的服務埠為7982,而該埠對應的正式prod環境配置

在瀏覽器中輸入測試url:http://localhost:7982/properties

ConfigProperties [author=xyh, age=27, sex=woman, time=20170123]
測試結果也對應。

總結:這種多環境配置在其他的應用場景中也是非常有用的,比如說,測試的時候,不希望調某個介面,不希望給某人發郵件等等,就可以用多環境配置進行區分!