SpringBoot默认加载的是application.yml文件,所以想要引入其他配置的yml文件,就要在application.yml中激活该文件

定义一个application-resources.yml文件(注意:必须以application-开头)

application.yml中:

  spring:
profiles:
active: resources

以上操作,xml自定义文件加载完成,接下来进行注入。

application-resources.yml配置文件代码:

 user:
filepath: 12346
uname: "13" admin:
aname: 26

方案一:无前缀,使用@Value注解

 @Component
//@ConfigurationProperties(prefix = "user")
public class User { @Value("${user.filepath}")
private String filepath;
@Value("${user.uname}")
private String uname; public String getFilepath() {
return filepath;
} public void setFilepath(String filepath) {
this.filepath = filepath;
} public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} @Override
public String toString() {
return "User{" +
"filepath='" + filepath + '\'' +
", uname='" + uname + '\'' +
'}';
}
}

方案二:有前缀,无需@Value注解

 @Component
@ConfigurationProperties(prefix = "user")
public class User { //@Value("${user.filepath}")
private String filepath;
//@Value("${user.uname}")
private String uname; public String getFilepath() {
return filepath;
} public void setFilepath(String filepath) {
this.filepath = filepath;
} public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} @Override
public String toString() {
return "User{" +
"filepath='" + filepath + '\'' +
", uname='" + uname + '\'' +
'}';
}
}

测试类:

 package com.sun123.springboot;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class UTest { @Autowired
User user; @Test
public void test01(){
System.out.println(user);
}
}

测试结果:

Springboot引入多个yml方法的更多相关文章

  1. SpringBoot使用@Value从yml文件取值为空--注入静态变量

    SpringBoot使用@Value从yml文件取值为空--注入静态变量     1.application.yml中配置内容如下:   pcacmgr:   publicCertFilePath: ...

  2. springboot引入thymeleaf

    springboot引入thymeleaf 1.Thymeleaf使用 @ConfigurationProperties(prefix = "spring.thymeleaf") ...

  3. jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法

    jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...

  4. 两种解决springboot 跨域问题的方法示例

    两种解决springboot 跨域问题的方法示例,哪种方法看情况而定,自己选择.社会Boolean哥,人狠话不多,直接上代码. 第一种实现方式:       此种方式做全局配置,用起来更方便,但是无法 ...

  5. SpringBoot 使用 RestTemplate 调用exchange方法 显示错误信息

    SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...

  6. springboot引入Oracle依赖

    最近学习spring boot,在网上找一些项目学习,有的项目引入了oracle驱动包,自己搭建一直不成功,百度发现说是权限问题无法下载. 然后参考下面博客终于解决:springboot引入Oracl ...

  7. SpringBoot读取外部配置文件的方法

    SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...

  8. springboot引入mybatis遇到的坑

      前边分享了springboot项目的创建及springboot项目的默认配置文件等,想温习的小伙伴可移步至文章末尾阅读,感谢.今天来分享下springboot引入mybatis框架的步骤,有小伙伴 ...

  9. SpringBoot static修饰的字段/方法如何获取application.yml配置

    SpringBoot的application.yml一种特殊的应用场景,一般我们获取application.yml的配置文件只要@Value就可以获取到值了,但是如果是static修饰的字段肯定就不能 ...

随机推荐

  1. Altium Designer 18 ------ 原理图和PCB元器件互相查找

    方法一:单击选中原理图中元器件,然后单击tools>Seclect PCBcomponents,即可在PCB中看到该器件的高亮显示: 方法二:单击Tools>Cross Select Mo ...

  2. wz

    chcp 437 monkeyrunner wz.py wz.bat #coding=utf-8 from com.android.monkeyrunner import MonkeyDevice,M ...

  3. 关于vue的小实例

    学习网址:http://www.runoob.com/vue2/vue-tutorial.html 下面是我在上面学着写的两个小例子, 1. 实现点击全选,下面的均被选中,再点击一下,下面的均取消选择 ...

  4. beego 实现API自动化文档

    安装beego和bee工具 1.beego安装 go get -u github.com/astaxie/beego 2.安装bee工具 go get -u github.com/beego/bee ...

  5. 全局拦截各种http请求

    http请求无非就是ajax.src.href.表单 function hookAJAX() { XMLHttpRequest.prototype.nativeOpen = XMLHttpReques ...

  6. Ambari集成Kerberos报错汇总

    Ambari集成Kerberos报错汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看报错的配置信息步骤 1>.点击Test Kerberos Client,查看相 ...

  7. maven构建myeclipse 工程

    前提:安装maven完成后 mvn -version查看版本 一,新建WEB 工程  mvn archetype:generate -DgroupId={project-packaging} -Dar ...

  8. blackeye部署

    部署: apt-get install php apt-get install libapache2-mod-php apt-get install git git clone https://git ...

  9. Linux学习笔记:【002】ARM指令流水线

    指令的处理 在CPU中,对于指令的处理一般分为: 1.取指令阶段 取指令(Instruction Fetch,IF)阶段是将一条指令从主存中取到指令寄存器的过程. 程序计数器PC中的数值,用来指示当前 ...

  10. NodeJs 学习笔记(一)Wedding 项目搭建

    说明:Ubuntu16.04 自带的NodeJs版本太低,安装包更新不了,只能编译安装了 一.NodeJs编译安装 下载:https://nodejs.org/en/download/ 修改目录权限: ...