springBoot02- 配置文件读取测试
1.照例登陆http://start.spring.io/ ,改个项目名(Artifact),然后下载导入Eclipse

2. 项目结构如下,

在pom中添加web依赖(不添加,就找不到RestController这个注解要导入的包了)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.编写2个配置文件,一个是application.properties,如下:
server.port=9090
server.servlet.context-path=/spring-boot
#server.context-path=/helloboot
book.author.name=tom
一个是application.yml ,如下
book:
author:
name1: JACKSON
4. 在com.lch.springboot02.domain 包下创建一个AuthorProperties 类,读取配置文件信息(同时读取2个配置文件,实际证明,springboot是可以同时读取两种格式的配置文件中的配置信息的),使用注解@Value("${配置文件中的属性名}")来读取配置文件,并设置对应属性接收,还需要设置set get方法
package com.lch.springboot02.domain; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class AuthorProperties { @Value("${book.author.name}")
private String authorName; @Value("${book.author.name1}")
private String authorName1; public String getAuthorName() {
return authorName;
} public void setAuthorName(String authorName) {
this.authorName = authorName;
} public String getAuthorName1() {
return authorName1;
} public void setAuthorName1(String authorName1) {
this.authorName1 = authorName1;
}
}
5. 在com.lch.springboot02.controller 包下建立contorller类 PropertiesController ,把读取到的配置信息返回给前台显示,这里需要注入 AuthorProperties 的实例
package com.lch.springboot02.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.lch.springboot02.domain.AuthorProperties; @RestController // 使用这个注解必须在pom中引入web模块的依赖!
public class PropertiesController {
@Autowired
AuthorProperties properties;
@RequestMapping("/hello")
public String hello() {
return "hello spring-boot";
} @RequestMapping("/author")
public String author() {
return "the author name is: " + properties.getAuthorName() + ", the author name1 is: " + properties.getAuthorName1();
} }
6. 启动应用,访问:http://localhost:9090/spring-boot/author ,结果如下:

之所以是这个请求路径,控制台告诉了我们:

在上一个入门例子中,应用默认端口是8080,context path 是空,这里发生了变化,这是因为springboot读取到了application.properties中配置的端口号和上下文路径(
server.port=9090 server.servlet.context-path=/spring-boot ),并进行了应用.
注意:spring boot 2.0.0的ContextPath配置有变化: server.servlet.context-path=/XXX ,之前是server.context-path=/xxx
在前端页面展示的结果中,看到了name1的值也有,说明application.yml配置文件中的信息也读取到了。
例子下载地址:
https://github.com/liuch0228/springboot.git
springBoot02- 配置文件读取测试的更多相关文章
- [spring源码学习]二、IOC源码——配置文件读取
一.环境准备 对于学习源码来讲,拿到一大堆的代码,脑袋里肯定是嗡嗡的,所以从代码实例进行跟踪调试未尝不是一种好的办法,此处,我们准备了一个小例子: package com.zjl; public cl ...
- C 构造一个 简单配置文件读取库
前言 最近看到这篇文章, json引擎性能对比报告 http://www.oschina.net/news/61942/cpp-json-compare?utm_source=tuicool 感觉技术 ...
- cocos2d-x开发: 一切应该从配置文件读取开始
想要做一款完整的游戏,应该从配置文件读取开始.cocos2d-x本身提供了UserDefault来操作xml格式的配置文件,准确的说配置这模块引擎开发者已经考虑到了.但是xml格式包含大量无关的格式信 ...
- Java配置文件读取中文乱码问题
背景 这是我之前在做的用友服务对接开发,昨天领导拿给财务测试时告诉我有乱码,当时我第一想法是用友那边的编码格式有问题,因为还在做其他任务,我说等问一下用友他们用的什么编码格式我们这边改一下,然后今天早 ...
- VS2012中,C# 配置文件读取 + C#多个工程共享共有变量 + 整理using语句
(一) C# 配置文件读取 C#工程可以自动生成配置文件,以便整个工程可以使用设置的配置进行后续的处理工作. 1. 首先,右键工程文件-->Properties -->settings-- ...
- C# 配置文件读取与修改
C# 配置文件读取与修改 配置文件在很多情况下都使用到, 配置文件分为两种 一种是应用程序的配置文件, 一种是web的配置文件. 两种配置文件最大的区别是web的配置文件更新之后会实时更新, 应用 ...
- java Spring使用配置文件读取jdbc.properties
Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer" ...
- C# 配置文件读取与修改(转)
C# 配置文件读取与修改 配置文件在很多情况下都使用到, 配置文件分为两种 一种是应用程序的配置文件, 一种是web的配置文件. 两种配置文件最大的区别是web的配置文件更新之后会实时更新, 应用 ...
- smarty 从配置文件读取变量
smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...
- Smarty从配置文件读取的变量
从配置文件读取的变量 配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到) 第二种语法在变量作为属性值并被引号括住的 ...
随机推荐
- nas请求响应示意图
curl nginx(proxy_connect) nginx(NAS) | | | | | | (1) |-- CONNECT 443 -> | | | | | | |---- [ TCP c ...
- docker 配置sonatype/nexus3
docker search nexusdocker pull sonatype/nexus3mkdir -p /dockermaven/nexus-datachmod -R 777 /dockerma ...
- statistics——数学统计函数
statistics——数学统计函数 转自:https://blog.csdn.net/zhtysw/article/details/80005410 资源代码位置:Lib/statistixs.py ...
- 关于static以及final关键字
Static关键字: 可以用来修饰类中的属性.类中的方法.以及具体的某一个类. 1.用于修饰属性: 则表示该属性属于整个类,不论有多少个对象实例,所有的实例共同拥有一个static静态的成员变量.该变 ...
- 19.ThreadLocal方法解决代码不友好的问题
#ThreadLocal import threading #创建全局ThreadLocal loacl_school = threading.local() class Student(): def ...
- 查询SQLSERVER执行过的SQL记录(历史查询记录)(转)
原文链接:https://www.cnblogs.com/icycore/p/10493237.html 有的时候,需要知道近段时间SQLSERVER执行了什么语句,可以用下面的方法: SELECT ...
- ES6 find 和 filter 的区别
ES6 find 和 filter 的区别 : 遇到个功能是要分类就想说在前端过滤,不要从查数据库的时候过滤了.然后就想说除了filter还有啥好用的 发现有个find,测试一番之后发现 const ...
- jvm学习(3)方法区、堆、对象存储位置
方法区 方法区,Method Area, 对于习惯在HotSpot虚拟机上开发和部署程序的开发者来说,很多人愿意把方法区称为“永久代”(Permanent Generation),本质上两者并不等价, ...
- 使用Angular2+的内置管道格式化数据
在简书看到一篇关于Angualr运用内置管道格式化数据的总结,感觉挺实用的,转载一下以供参考: [转载]https://www.jianshu.com/p/a8bd5a1d2c53 PS:管道是在HT ...
- JSONP面试
jQuery 的 JSONP的原理是动态创建一个 script 标签,利用src 发送请求,获取数据 回调函数的键名叫做 callback 跟ajax没有关系 JSONP:主要是利用 script标 ...