Springboot读取自定义配置文件的几种方法
一、读取核心配置文件
核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties内容如下:
server.port=
test.msg=Hello World Springboot!
1、使用@Value方式(常用):
@RestController
public class WebController { @Value("${test.msg}")
private String msg; @RequestMapping(value = "index", method = RequestMethod.GET)
public String index() {
return "The Way 1 : " +msg;
}
}
注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:9090/index 时将得到The Way 1 : Hello World Springboot!
2、使用Environment方式
@RestController
public class WebController { @Autowired
private Environment env; @RequestMapping(value = "index2", method = RequestMethod.GET)
public String index2() {
return "The Way 2 : " + env.getProperty("test.msg");
}
}
注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。
二、读取自定义配置文件
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources/config目录下创建配置文件my-web.properties
resources/config/my-web.properties内容如下:
web.name=zslin
web.version=V 1.0
web.author=@qq.com
1、创建管理配置的实体类:
需要用到2个注解:@ConfigurationProperties
@Component,把该类变成spring的一个组件
@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web")
@Component
public class MyWebConfig { private String name;
private String version;
private String author; public String getAuthor() {
return author;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public void setAuthor(String author) {
this.author = author;
}
public void setName(String name) {
this.name = name;
}
public void setVersion(String version) {
this.version = version;
}
}
注意:
(1)在@ConfigurationProperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以web.开头)
(2)使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。
2、创建测试Controller
@RestController
@RequestMapping(value = "config")
public class ConfigController { @Autowired
private MyWebConfig myWebConfig; @RequestMapping(value = "index", method = RequestMethod.GET)
public String index() {
return "webName: "+myWebConfig.getName()+", webVersion: "+
myWebConfig.getVersion()+", webAuthor: "+myWebConfig.getAuthor();
}
}
注意:由于在MyWebConfig类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。
访问:http://localhost:9090/config/index 时将得到webName: zslin, webVersion: V 1.0, webAuthor: 393156105@qq.com
Springboot读取自定义配置文件的几种方法的更多相关文章
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
- 【转载】java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...
- java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...
- SpringBoot读取自定义配置文件
自定义配置文件 my-config.properties bfxy.title=bfxy bfxy.name=hello spring boot! bfxy.attr=12345 配置文件类 appc ...
- 2017.6.29 java读取.properties配置文件的几种方法
参考来自:http://www.cnblogs.com/s3189454231s/p/5626557.html 关于路径的解释:http://blog.csdn.net/bluishglc/artic ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
随机推荐
- JZYZOJ 2043 多项式除法和取余 NTT 多项式
http://172.20.6.3/Problem_Show.asp?id=2043 最开始用了FFT,交上去全tle和wa了(tle的比较多),测了一组数据发现求逆元的过程爆double了(毕竟系数 ...
- 【优先队列+贪心】BZOJ1826-[JSOI2010]缓存交换
……啊开始颓了. [题目大意] 已知当前集合最大容量为m,n个询问.每次询问一个元素,如果集合中没有则需要加入该元素,如果集合已经满了则需要先删去集合中的某些元素再加入.问至少要加入几次元素? [思路 ...
- 【BZOJ-2063】我爸是李刚 数位dp 好题
2063: 我爸是李刚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 139 Solved: 72[Submit][Status][Discuss] ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- LPC43xx SGPIO Slice 示意图
SGPIO inverted clock qualifier Hi, With bits 6:5 of SGPIO_MUX_CFG the QUALIFIER_MODE is selected (0x ...
- LabTool : LPC LINK2, LPC4370 cheap scope: 80Ms/s 12 bit
80MHz 12 bit ADC processor LPC4370.LPCxpresso do a LPC LINK2 and LABTOOLS open source oscilloscope d ...
- STM32 Timer : Base Timer, Input Capture, PWM, Output Compare
http://www.cs.indiana.edu/~geobrown/book.pdf An example of a basic timer is illustrated in Figure 10 ...
- Microsoft OS Descriptors
Microsoft OS Descriptors Updated: April 11, 2014 USB devices store standard descriptors in firmware ...
- mozilla/rr 调试
http://rr-project.org/ https://github.com/mozilla/rr
- xamarin 断点 不命中
Async Debugging Breakpoints not being hit breakpoint in Android library project not hit when disable ...