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配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
随机推荐
- JDBC(三)数据库连接池(DBCP、C3P0)
前言 这段时间状态有一点浮躁,希望自己静下心来.还有特别多的东西还没有学懂.需要学习的东西非常的多,加油! 一.JDBC复习 Java Data Base Connectivity,java数据库连接 ...
- poj 2566 Bound Found
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4384 Accepted: 1377 Spe ...
- [Bayesian] “我是bayesian我怕谁”系列 - Boltzmann Distribution
使用Boltzmann distribution还是Gibbs distribution作为题目纠结了一阵子,选择前者可能只是因为听起来“高大上”一些.本章将会聊一些关于信息.能量这方面的东西,体会“ ...
- ③bootstrap文本使用基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML5 文件上传
这篇随笔主要引用https://juejin.im/post/59598ecf5188250d8d141fff,只用于自己学习,不对外宣传. FileList 对象和 file 对象 input[ty ...
- SUID,SGID,SBIT这些到底是什么
SUID,SGID,SBIT这些都是文件的特殊权限. SUID(Set UID)文件执行过程中,用户拥有文件的root权限. SGID(Set GID)文件执行过程中,执行者拥有该文件的用户组的权限. ...
- Getting Started With setuptools and setup.py
https://pythonhosted.org/an_example_pypi_project/setuptools.html http://www.ianbicking.org/docs/setu ...
- RabbitMQ消息队列系列教程(一)认识RabbitMQ
摘要 RabbitMQ是最为流行的消息中间件,是处理高并发业务的利器.本系列教程,将跟大家一起学习RabbitMQ. 目录 RabbitMQ是什么? RabbitMQ的特点是什么? 一.RabbitM ...
- PHP中MD5函数漏洞
题目描述 一个网页,不妨设URL为http://haha.com,打开之后是这样的 if (isset($_GET['a']) and isset($_GET['b'])) { if ($_GET[' ...
- Python网络编程篇之select和epoll
1. select 原理 在多路复⽤的模型中, ⽐较常⽤的有select模型和epoll模型. 这两个都是系统接⼝, 由操作系统提供. 当然, Python的select模块进⾏了更⾼级的封装. ⽹络 ...