GitHub
src="//ghbtns/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">需求产生
开发过程中可能会有如下需求:开发和部署的配置信息可能不同,常规的方式就是在配置文件里面先写好开发配置,在部署的时候再去修改这些配置,这样可能会出现很多问题,比如用户名、密码忘记了修改或者改错了等问题。
项目结构图片
application.properties
spring.profiles.active=test
核心配置文件里面的spring.profiles.active=test这个设置表示系统默认使用test(即application-test.properties)中的配置。
application-test.properties
jdbc.username=rootTest
jdbc.password=rootTest
application-dev.properties
jdbc.username=rootDev
jdbc.password=rootDev
MultipartPropertiesTest1.java
package com.jege.spring.boot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author JE哥
* @email 1272434821@qq
* @description:
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MultipartPropertiesTest1 {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Test
public void config() {
System.out.println("当前的用户名是: " + username);
System.out.println("当前的密码是: " + password);
}
}
MultipartPropertiesTest2.java
package com.jege.spring.boot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author JE哥
* @email 1272434821@qq
* @description:
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")
public class MultipartPropertiesTest2 {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Test
public void config() {
System.out.println("当前的用户名是: " + username);
System.out.println("当前的密码是: " + password);
}
}
其他关联项目
- Spring Boot 菜鸟教程 13 注解定时任务
http://blog.csdn/je_ge/article/details/53434227 - Spring Boot 菜鸟教程 7 EasyUI-datagrid
http://blog.csdn/je_ge/article/details/53365189
源码地址
https://github/je-ge/spring-boot
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
更多推荐
Spring Boot 菜鸟教程 28 多配置文件
发布评论