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修饰的字段肯定就不能 ...
随机推荐
- mac 切换用户
sh-3.2# su - houzhibinhouzhibindeMacBook-Pro:~ houzhibin$
- [Discuz!] Discuz X1.5点击“发帖”出现XML代码的解决办法!
使用的是Discuz X1.5程序,不知什么原因,今天突然出现了大问题,就是在点击“发帖”的时候,原来正常显示的网页竟然变成了XML代码!经过一番查找资料,也未能找到是什么原因导致的,只是找到了解决办 ...
- nginx location反向代理不对等时的处理
server{ server_name git.cheyunhua.top; location /test12/ { proxy_pass https://www.baidu.com/;}} loca ...
- 安装pycrypto2.6.1报错
C:\Users\xxx\Downloads\pycrypto-2.6.1\pycrypto-2.6.1>python setup.py install running install runn ...
- Xstart Insatll And Usage
不进入 Linux 桌面环境,又需要运行一些图形化的软件,比如 Oracle 数据库的安装等 安装 Windows 上 Xstart 安装:https://www.cnblogs.com/jhxxb/ ...
- JAVA核心技术I---JAVA回顾
一:基础类型运算 大部分的指令都没有支持byte.char.short,没有任何指令支持boolean类型.编译器在编译期或者运行期将byte和short类型的数据带符号扩展为相应的int类型数据,将 ...
- 浏览器内核 Trident Gecko Presto Webkit
Trident内核:IE,MaxThon,TT,The World,360,搜狗浏览器等 Gecko内核:Netscape6及以上版本,FF,MozillaSuite/SeaMonkey等 Prest ...
- jquery load 陷阱【原】
基础写法 function load(targetId,templateName) { $("#"+targetId).load(contextPath+templateName) ...
- VMware Workstation Pro 14 虚拟机安装教程
准备学习一下linxus,可不想双系统,那么只有虚拟机这条路了.故在此记录一下安装VMware的过程. 一.下载VMware 我用的是VMware Workstation Pro 14(其他版本也可以 ...
- Win10升级.NET Framework 3.5或2.0遇到错误0x800f081f怎么办
为了让win10系统更好地运行游戏,很多玩家都会在电脑中安装.NET Framework 3.5或.NET Framework 2.0.不过,部分用户在更新升级.NET Framework 3.5和2 ...