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- 配置文件读取测试的更多相关文章

  1. [spring源码学习]二、IOC源码——配置文件读取

    一.环境准备 对于学习源码来讲,拿到一大堆的代码,脑袋里肯定是嗡嗡的,所以从代码实例进行跟踪调试未尝不是一种好的办法,此处,我们准备了一个小例子: package com.zjl; public cl ...

  2. C 构造一个 简单配置文件读取库

    前言 最近看到这篇文章, json引擎性能对比报告 http://www.oschina.net/news/61942/cpp-json-compare?utm_source=tuicool 感觉技术 ...

  3. cocos2d-x开发: 一切应该从配置文件读取开始

    想要做一款完整的游戏,应该从配置文件读取开始.cocos2d-x本身提供了UserDefault来操作xml格式的配置文件,准确的说配置这模块引擎开发者已经考虑到了.但是xml格式包含大量无关的格式信 ...

  4. Java配置文件读取中文乱码问题

    背景 这是我之前在做的用友服务对接开发,昨天领导拿给财务测试时告诉我有乱码,当时我第一想法是用友那边的编码格式有问题,因为还在做其他任务,我说等问一下用友他们用的什么编码格式我们这边改一下,然后今天早 ...

  5. VS2012中,C# 配置文件读取 + C#多个工程共享共有变量 + 整理using语句

    (一) C# 配置文件读取 C#工程可以自动生成配置文件,以便整个工程可以使用设置的配置进行后续的处理工作. 1. 首先,右键工程文件-->Properties -->settings-- ...

  6. C# 配置文件读取与修改

    C# 配置文件读取与修改   配置文件在很多情况下都使用到, 配置文件分为两种 一种是应用程序的配置文件, 一种是web的配置文件. 两种配置文件最大的区别是web的配置文件更新之后会实时更新, 应用 ...

  7. java Spring使用配置文件读取jdbc.properties

    Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer" ...

  8. C# 配置文件读取与修改(转)

    C# 配置文件读取与修改   配置文件在很多情况下都使用到, 配置文件分为两种 一种是应用程序的配置文件, 一种是web的配置文件. 两种配置文件最大的区别是web的配置文件更新之后会实时更新, 应用 ...

  9. smarty 从配置文件读取变量

    smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...

  10. Smarty从配置文件读取的变量

    从配置文件读取的变量 配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到) 第二种语法在变量作为属性值并被引号括住的 ...

随机推荐

  1. Day1 方法的重载

    方法的重载 方法的标识符包括: 1.方法名称   2.方法的参数(参数类型和参数的数量) 方法的重载:方法名称相同 但是方法参数不同(1.参数类型不同 2.参数类型相同 但是参数个数不同 3.类型和个 ...

  2. postman使用动态token发post请求小结

    最近使用postman做接口测试,感觉挺好用的. 测试中,每次post请求都要携带一个token,token是通过get请求得来的,动态变化的,并且token有有效期的限制.为了避免重复获取token ...

  3. .NETFramework:System.Collections.Generic.KeyValuePair.cs

    ylbtech-.NETFramework:System.Collections.Generic.KeyValuePair.cs 定义可设置或检索的键/值对 1.返回顶部 1. #region 程序集 ...

  4. Python3.5自带venv创建虚拟环境

    为每个程序单独创建虚拟环境可以保证程序只能访问虚拟环境中的包,保持全局解释器的干净整洁,使其只作为创建(更多)虚拟环境的源. windows下创建虚拟环境 Python3.5自带venv,只需执行py ...

  5. Invoke和BeginInvoke的区别(转载)

    转自http://www.cnblogs.com/c2303191/articles/826571.html Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线 ...

  6. rpm --qf 命令

    1. 环境准备: sudo apt-get install rpm (Ubuntu系统) wget ftp://rpmfind.net/linux/fedora-secondary/developme ...

  7. JS对象—字符串总结(创建、属性、方法)

    1.创建字符串     1.1 new String(s)         String和new一起使用,创建的是一个字符串对象,存放的是字符串s的表示.     1.2 String(s)     ...

  8. linux flock()

    表头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依参数operation所指 ...

  9. Docker 换源

    近几天又折腾起 docker来了    我发现自己在拉镜像的时候,总是超时    然后百度了一下  说要换源 90sec的一个水友 推荐了我 阿里云的加速源    我看了还是免费就想试一下 讲一下过程 ...

  10. 用vultr搭建ss服务器的脚本

    原文在此