Spring Boot 菜鸟教程 28 多配置文件

编程知识 行业动态 更新时间:2024-06-13 00:22:04

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 多配置文件

本文发布于:2023-04-01 16:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/7f8a6e7b24c74196dabdf187d46eb55d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜鸟   配置文件   教程   Spring   Boot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!