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. 关于C(n,m) 的奇偶 ,与C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数

    (n & m) == m  为奇数 C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数 第一种想法是Lucas定理推导,我们分析一下 C(n,m)%2,那么由lucas定 ...

  2. P1541乌龟棋

    传送 这题咋做? 当然是爆搜了. 但是蒟蒻不会爆搜(TLE,WA两开花qwq),更不会记忆化搜索,所以我们换个思路. 注意这句话: 这肯定是有用的(洛咕还不会闲圈到给一句毛用都没有的话),那它有什么用 ...

  3. 深入学习Keras中Sequential模型及方法

    https://www.cnblogs.com/wj-1314/p/9579490.html

  4. virtualenv-windows下排坑

    1. 安装 pip install virtualenv pip install virtualenvwrapper-win    (win下一定要有这个 -win,不然后续 workon,mkvir ...

  5. Spring数据库连接池 c3p0、dbcp、spring-jdbc

    在用dbcp的时候 后面加上 destroy-method="close" 销毁的方法没事 但是用 spring的jdbc就会报错 提示找不到close这个方法  这是为什么? D ...

  6. json字符串格式

    private static final String COMPLEX_JSON_STR = "{" + "\"teacherName\":\&quo ...

  7. ubuntu下修改子网掩码

    1.修改网络配置 修改 /etc/netplan/01-network-manager-all.yaml 文件 vi /etc/netplan/01-network-manager-all.yaml ...

  8. Java常见问题收集

    转载处:https://blog.csdn.net/jackfrued/article/details/44921941 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽 ...

  9. rancher部署K8S

    环境:centos7 docker 日期准确 关闭防火墙 安装docker 创建 vim /etc/docker/daemon.json {    "registry-mirrors&quo ...

  10. Codeforces - 1195D2 - Submarine in the Rybinsk Sea (hard edition) - 组合数学

    https://codeforc.es/problemset/problem/1195/D2 很明显可以看出,任意一个长度为\(l_1\)的数串\(s_1\)和任意一个长度为\(l_2\)的数串\(s ...