Springboot读取配置文件及自定义配置文件
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读取配置文件及自定义配置文件的更多相关文章
- springboot读取properties和yml配置文件
一.新建maven工程:springboot-configfile-demo,完整工程如下: pom.xml <?xml version="1.0" encoding=&qu ...
- SpringBoot之加载自定义配置文件
SpringBoot默认加载配置文件名为:application.properties和application.yml,如果需要使用自定义的配置文件,则通过@PropertySource注解指定. J ...
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- Springboot-读取核心配置文件及自定义配置文件
读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 核心 ...
- springboot读取自己定义的配置文件的方式以及使用joda_time来处理时间日期
总的来说呢,有两种方式,一种是原始的方式,即使用PropertiesUtils来读取配置文件. 第二种就是使用springboot的注解的方式来读取配置文件. 1.原始方式处理属性和时间日期: 工具类 ...
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- Springboot 之 自定义配置文件及读取配置文件注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以(下划线的坑,坑了我两天..特此纪念)
注意:配置文件中的字符串不要有下划线 .配置中 key不能带下划线,value可以 错误的.不能读取的例子: mySet .ABAP_AS_POOLED = ABAP_AS_WITH_P ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
随机推荐
- session设置过期的方法(转载)
这篇文章主要介绍了php中实现精确设置session过期时间的方法,需要的朋友可以参考下 大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设 ...
- Github和Github for windows的使用简介
很多程序员都把自己开发的代码放到Github上,方便自己管理也有利于别人查阅.所以这两天我也捣鼓了一下这个东西,现在把怎么使用Github和Github for windows简单的总结一下. 1.现 ...
- Pendant
Pendant Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- css基础语法二(常用文本与背景属性)
[CSS常用文本属性] 1. 字体.字号类:① font-weight: 字体粗细. bold-加粗.normal-正常.lighter-细体 也可以使用100-900数值,400表示normal,7 ...
- ie 如何判断正在执行的脚本
1.在非ie浏览器里,script上的onload事件会准确地在脚本执行完以后触发 2.在ie浏览器里,如果脚本是在缓存里的话,那么onload事件是在你把script标签插入Dom结束时马上执行 ...
- ⒁bootstrap组件 工具提示框 弹出框 警告框 基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux 新建文件/文件夹,删除文件文件夹,查找文件 打开文件
1.新建文件夹:mkdir xx 2.新建文件: touch 1.py 3.删除文件/文件夹: rm -r xx rm 1.py 4.打开文件:cat 1.py 只显示前几行 :head -2 1. ...
- SpringMVC知识一锅烩
Spring简介 SpringMVC和Struts2一样都是属于表现层的框架,将前段发出的请求分发给对应的后端处理器即Controller 处理流程 用户请求被前端控制前拦截,然后根据对应的拦截路径去 ...
- windows下配置wampserver
第一,打开局域网访问 配置文件:点击右下角的WAMP 服务器小托盘,选择Apache>httpd.conf 搜索关键字 "deny from ",会发现一处 "de ...
- 一款非常推荐的用户界面插件----EasyUI
前 言 easyui是一种基于jQuery的用户界面插件集合. easyui为创建现代化,互动,JavaScript应用程序,提供必要的功能. 使用easyui你不需要写很多代码,你只需要 ...