1.创建maven工程,在pom文件中添加依赖

   <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 单元测试使用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency> </dependencies>

2.创建项目启动类 StartApplication.java

 package com.kelly.controller;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@EnableAutoConfiguration //自动加载配置信息
@ComponentScan("com.kelly")//使包路径下带有注解的类可以使用@Autowired自动注入
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}

3.编辑配置文件application.properties及自定义配置文件define.properties

  application.properties

#访问的根路径
server.context-path=/springboot
#端口号
server.port=8081
#session失效时间
server.session-timeout=30
#编码
server.tomcat.uri-encoding=utf-8 test.name=kelly
test.password=admin123

  define.properties

defineTest.pname=test
defineTest.password=test123

4.读取application.properties配置文件中的属性值

  FirstController.java

 package com.kelly.controller;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class FirstController { @Value("${test.name}")
private String name; @Value("${test.password}")
private String password; @RequestMapping("/")
@ResponseBody
String home()
{
return "Hello Springboot!";
} @RequestMapping("/hello")
@ResponseBody
String hello()
{
return "name: " + name + ", " + "password: " + password;
}
}

5.打开浏览器,输入 http://localhost:8081/springboot/hello 即可看到结果

6.使用java bean的方式读取自定义配置文件 define.properties

  DefineEntity.java

 package com.kelly.entity;

 import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix="defineTest")
@PropertySource("classpath:define.properties")
public class DefineEntity { private String pname; private String password; public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

  SecondController.java

 package com.kelly.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.kelly.entity.DefineEntity; @Controller
public class SecondController { @Autowired
DefineEntity defineEntity; @RequestMapping("/define")
@ResponseBody
String define()
{
return "test.name:" + defineEntity.getPname() + ", test.password:" + defineEntity.getPassword();
}
}

7.打开浏览器,访问 http://localhost:8081/springboot/define,可以看到输出结果

补充:我的项目的目录结构

如果遇到问题也可以留言,我如果看到的话,不管会不会都会给与回复的,我们可以共同讨论,一起学习进步。

Springboot读取配置文件及自定义配置文件的更多相关文章

  1. springboot读取properties和yml配置文件

    一.新建maven工程:springboot-configfile-demo,完整工程如下: pom.xml <?xml version="1.0" encoding=&qu ...

  2. SpringBoot之加载自定义配置文件

    SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定. J ...

  3. SpringBoot系列——加载自定义配置文件

    前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...

  4. Springboot-读取核心配置文件及自定义配置文件

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

  5. springboot读取自己定义的配置文件的方式以及使用joda_time来处理时间日期

    总的来说呢,有两种方式,一种是原始的方式,即使用PropertiesUtils来读取配置文件. 第二种就是使用springboot的注解的方式来读取配置文件. 1.原始方式处理属性和时间日期: 工具类 ...

  6. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

  7. Springboot 之 自定义配置文件及读取配置文件

    本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...

  8. Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)

    注意:配置文件中的字符串不要有下划线 .配置中  key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED      =  ABAP_AS_WITH_P ...

  9. Springboot读取自定义配置文件的几种方法

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

随机推荐

  1. bug:逆向思维的延伸

    哈哈,我又来了. 前段时间自己出了一个bug,说起来也属于比较常见的类型 A业务需要做一个活动,需要B业务判断,如果是通过A业务跳转至B业务的用户,则在B页面给这类用户展示一个对应的弹窗.(A是新增业 ...

  2. 机器学习之三:logistic回归(最优化)

    一般来说,回归不用在分类问题上,因为回归是连续型模型,而且受噪声影响比较大.如果非要应用进入,可以使用logistic回归. logistic回归本质上是线性回归,只是在特征到结果的映射中加入了一层函 ...

  3. 如何让Vim成为我们的神器

    Vim 是 Linux 系统上的最著名的文本/代码编辑器,也是早年的 Vi 编辑器的加强版,而 gVim 则是其 Windows 版.它的最大特色是完全使用键盘命令进行编辑,脱离了鼠标操作虽然使得入门 ...

  4. Problem E

    题意:看电视,计算出最多看多少个电视,已给出电视起始终止时间: 解体思路:思路这个题拿到手没多想,上课的例题,就照葫芦画瓢写了一个: 感悟:虽然刚开始学贪心,第一遍代码就AC了有点小小的成就感: 代码 ...

  5. awk巩固练习题

    第1章 awk数组练习题 1.1 文件内容(仅第一行) [root@znix test]# head -1 secure-20161219 access.log ==> secure-20161 ...

  6. jumpserver安装详解

    环境说明 主机为最小 安装的centos6.9 x86_64. [root@m01 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [ro ...

  7. JS是按值传递还是按引用传递?【转载】

    最近遇到个有趣的问题:“JS中的值是按值传递,还是按引用传递呢?” 在分析这个问题之前,我们需了解什么是按值传递(call by value),什么是按引用传递(call by reference). ...

  8. Problem C: 学生的排序

    Problem C: 学生的排序 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 274  Solved: 136[Submit][Status][Web ...

  9. 深入理解ES6之—set与map

    Set是无重复值的有序列表.Set会自动移除重复的值,因此你可以使用它来过滤数组中重复的值并返回结果. Map是有序的键值对,其中的键允许是任何类型. Set和Map是es6新增的两个数据集合. Se ...

  10. 全面理解 ASP.NET Core 依赖注入

    DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关于依赖注入的概念,身边有工作六七年的同事还个东西搞不清楚.另外再介绍一下.NET  Core的DI实现以及对实例 ...