博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot读取自定义配置文件的几种方法
阅读量:3899 次
发布时间:2019-05-23

本文共 2546 字,大约阅读时间需要 8 分钟。

一、读取核心配置文件

  核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

核心配置文件application.properties内容如下:

server.port=9090

test.msg=Hello World Springboot!
1、使用@Value方式(常用):

@RestControllerpublic 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方式

@RestControllerpublic 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=zd

web.version=V 1.0
web.author=1103387724@qq.com
1、创建管理配置的实体类:

需要用到2个注解:@ConfigurationProperties

@Component,把该类变成spring的一个组件

@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web")@Componentpublic 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://nnsen.baihongyu.com/

你可能感兴趣的文章
使用systemtap调试linux内核
查看>>
SystemTap Beginner
查看>>
深入分析 Linux 内核链表
查看>>
qperf测量网络带宽和延迟
查看>>
linux kernel笔记——中断
查看>>
linux 内核tcp接收数据的实现
查看>>
gcc 生成 .a静态库和 .so动态库
查看>>
三张图看遍Linux 性能监控、测试、优化工具
查看>>
100个常用的linux命令
查看>>
高效Linux用户需要了解的命令行技能
查看>>
TCP恋爱史:三次握手和四次分手
查看>>
Linux 网络堆栈的排队机制
查看>>
Linux网络协议栈之数据包处理过程(非技术人员勿看)
查看>>
多队列网卡简介以及Linux通过网卡发送数据包源码解读
查看>>
linux内核网络监听哈希表介绍:如何将sk加入表和将sk移除表的过程
查看>>
linux 笔记 关于struct file 结构下 private_data 数据结构的思考
查看>>
linux cpu:理解Linux系统负荷
查看>>
systemtap函数调用栈信息不齐的原因和解决方法 :print_backtrace
查看>>
解决git clone提示Permission denied publickey 问题
查看>>
GNU C - 一个别致的HelloWorld程序 引申到: __attribute__((constructor)|(destructor)(PRIORITY))
查看>>