Springboot引入多个yml方法
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方法的更多相关文章
- SpringBoot使用@Value从yml文件取值为空--注入静态变量
SpringBoot使用@Value从yml文件取值为空--注入静态变量 1.application.yml中配置内容如下: pcacmgr: publicCertFilePath: ...
- springboot引入thymeleaf
springboot引入thymeleaf 1.Thymeleaf使用 @ConfigurationProperties(prefix = "spring.thymeleaf") ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- 两种解决springboot 跨域问题的方法示例
两种解决springboot 跨域问题的方法示例,哪种方法看情况而定,自己选择.社会Boolean哥,人狠话不多,直接上代码. 第一种实现方式: 此种方式做全局配置,用起来更方便,但是无法 ...
- SpringBoot 使用 RestTemplate 调用exchange方法 显示错误信息
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- springboot引入Oracle依赖
最近学习spring boot,在网上找一些项目学习,有的项目引入了oracle驱动包,自己搭建一直不成功,百度发现说是权限问题无法下载. 然后参考下面博客终于解决:springboot引入Oracl ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
- springboot引入mybatis遇到的坑
前边分享了springboot项目的创建及springboot项目的默认配置文件等,想温习的小伙伴可移步至文章末尾阅读,感谢.今天来分享下springboot引入mybatis框架的步骤,有小伙伴 ...
- SpringBoot static修饰的字段/方法如何获取application.yml配置
SpringBoot的application.yml一种特殊的应用场景,一般我们获取application.yml的配置文件只要@Value就可以获取到值了,但是如果是static修饰的字段肯定就不能 ...
随机推荐
- (计算几何 线段判交) 51nod1264 线段相交
1264 线段相交 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交). 如果相交,输出"Yes",否则输出"No". ...
- (暴力求解)Encoding HDU1020
Encoding 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1020 Time Limit: 2000/1000 MS (Java/Others) ...
- 110道python面试题
1.一行代码实现1--100之和 利用sum()函数求和 2.如何在一个函数内部修改全局变量 利用global 修改全局变量 3.列出5个python标准库 os:提供了不少与操作系统相关联的函数 s ...
- C# 封装SqlBulkCopy,让批量插入更方便
关于 SqlServer 批量插入的方式,前段时间也有大神给出了好几种批量插入的方式及对比测试(http://www.cnblogs.com/jiekzou/p/6145550.html),估计大家也 ...
- angular中因异步问题产生的错误解决方法
方法一 private userTaskList(){ let auth = this.make_basic_auth("kermit","kermit"); ...
- linux ssh 报错failed - POSSIBLE BREAK-IN ATTEMPT
linux ssh 报错failed - POSSIBLE BREAK-IN ATTEMPT 问题故障: 今天在新租的虚拟机上,发现ssh登陆机器的时候报错,如下: [root@pictures_ne ...
- 使用mutt自动发送邮件
1.Mutt安装及环境配置 1.1.安装 sudo yum install mutt 比如你要设置邮件的发信人,需要做: sudo vim /etc/Muttrc set envelope_from= ...
- 给笔记本更换SSD硬盘
给笔记本更换SSD硬盘... ---------- 给笔记本更换SSD硬盘 带活动字样的一个新的系统盘,一个之前的主分区的系统盘 ----------------------------
- ASP.NET的checkboxlist,RadioButtonList,FileUpload,image控件的综合使用
一.拖控件 控件拖完如图所示:(有些是对不齐的,加一个表单就好了,把他们都放在表单里) 单纯的RadioButton是不能只选择一个的,除非你手动给他们加上代码: RadioButton1.Group ...
- JAVA-集合类型List(ArrayList、LinkedList)常用操作例子(基础必备)
package com.net.xinfang.reflect; import java.util.ArrayList; import java.util.Arrays; import java.ut ...