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修饰的字段肯定就不能 ...
随机推荐
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- mysql优化好文
https://segmentfault.com/a/1190000006158186
- node(基础)_node中的javascript
一.前言 ...
- zend studio报错
Problems encountered while setting project description. Cannot create linked resource '/.org.eclipse ...
- 常用的一些cmd命令
常用的一些cmd命令总结 ----------- 1.ping主机名字,类似于ping机子的IP地址 2.查看当前用户的dos命令 3.查看机器名 调出计算器命令:calc 调出远程桌面的命令:mst ...
- Go 的构建模式
Go 的八种 Build Mode exe (静态编译) exe (动态链接 libc) exe (动态链接 libc 和非 Go 代码) pie 地址无关可执行文件(安全特性) c-archive ...
- kafka命令使用
1.创建 topic /usr/local/kafka/bin/kafka-topics.sh --create --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 - ...
- 利用salt搭建hadoop集群
自动化工具有很多..今天总结一下salt安装hadoop 步骤,学习过程. 1,机器列表 hosts文件 只需要将namenode的两台机器上配置 ,不解释了. 2.salt-master在10 ...
- 【SQL】SQL中on条件与where条件的区别
#前言 数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1.on条件是在生成临时表时 ...
- layui(六)——upload组件常见用法总结
layui中提供了非常简单的文件上传组件,这里写了一个上传图片的栗子,上传成功后返回图片在服务器的路径,并设置为页面中img的src属性.因为上传十分简单,没什么可说的,就直接上代码了. html代码 ...