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. codesmith 连接mysql

    下载地址是http://dev.mysql.com/downloads/mirror.php?id=403020 请先注册登录后才能下载mysql-connector-net-6.3.7.msi这个文 ...

  2. Luogu P3521 [POI2011]ROT-Tree Rotations

    题目链接 \(Click\) \(Here\) 线段树合并,没想到学起来意外的很简单,一般合并权值线段树. 建树方法和主席树一致,即动态开点.合并方法类似于\(FHQ\)的合并,就是把两棵树的信息整合 ...

  3. Luogu P2743 [USACO5.1]乐曲主题Musical Themes

    链接 \(Click\) \(Here\) 人生第一道后缀数组的题目.首先要对输入的串进行差分处理,差分后长度为(\(n - 1\))的相同子段就是原串中长度为\(n\)的相同(可变调)子段.求出来\ ...

  4. vue init 解决办法

    /由于windows系统的某方面问题,vue脚手架安装可能会出现第一证书丢失 // 报错:vue-cli · Failed to download repo vuejs-templates/webpa ...

  5. Jz2440开发板熟悉

    title: Jz2440开发板熟悉 tags: ARM date: 2018-10-14 15:05:56 --- 概述 外部晶振为12M Nand Flash 256M,Nor Flash 2M, ...

  6. LSTM时间序列预测及网络层搭建

    一.LSTM预测未来一年某航空公司的客运流量 给你一个数据集,只有一列数据,这是一个关于时间序列的数据,从这个时间序列中预测未来一年某航空公司的客运流量.数据形式: 二.实战 1)数据下载 你可以go ...

  7. Linux系统IO分析工具之iotstat常用参数介绍

    Linux系统IO分析工具之iotstat常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.安装iostat [root@flume115 ~]# yum - ...

  8. dubbo的具体使用

    dubbo的具体使用: dubbo简介: 1.1.    什么是dubbo 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,急需一个治理 ...

  9. C#下RSA算法的实现(适用于支付宝和易宝支付)

    RSA算法代码: using System; using System.Collections.Generic; using System.Text; using System.IO; using S ...

  10. python模块之os sys shutil

    os模块 os模块是与操作系统交互的一个接口 #当前执行这个python文件的工作目录相关的工作路径 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir( ...