一、说明

    在我们的日常开发中,生产环境的配置和测试环境的配置以及开发环境的配置基本上都是不相同的,每次到部署环境的时候,就需要手动的切换配置文件,如果在切换的过程中一不小心的话,就会出错,所以在开发中,一般会搞个配置文件检查的功能,来避免出错,而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]

测试结果也对应。

总结:这种多环境配置在其他的应用场景中也是非常有用的,比如说,测试的时候,不希望调某个接口,不希望给某人发邮件等等,就可以用多环境配置进行区分!