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配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
随机推荐
- 异步API
整合到一起,作为参考: //2 5 3 4 1 //2 5 3 1 4 var img = new Image(); img.src = '.'; img.onerror = function() { ...
- hive1.2.2部署
1.解压hvie.tar,进入conf目录下,cp hive-default.xml.template hive-site.xml; 2.将hive下的新版本jline的JAR包拷贝到hadoop下: ...
- 入我新美大的Java后台开发面试题总结
静儿最近在总结一些面试题,那是因为做什么事情都要认真.面试也一样,静儿作为新美大金融部门的面试官,负责任的告诉大家,下面的问题回答不上来,面试是过不了的.不过以下绝不是原题,你会发现自己实力不过硬,最 ...
- NFS存储服务部署
第1章 NFS介绍 1.1 NFS服务内容的概述 □ RPC服务知识概念介绍说明,以及RPC服务存在价值(必须理解掌握) □ NFS服务工作原理讲解(必须理解掌握) □ NFS共享文件系统使用原理讲解 ...
- Flex 布局实例
如图: 代码如下: <!DOCTYPE HTML> <html> <meta charset="utf-8"> <head> < ...
- PostgreSQL 下生成 UUID(Guid)
最近在Windows 10 下安装了 PostgreSQL(postgresql-9.6.3-1-windows.exe),在学习过程中,发现PostgreSQL 支持UUID(Guid)类型,但是却 ...
- Centos下配置tomcat7的https证书
近期搞定了HTTPS配置,特此记录. 1.把下载的文件拷贝到cert文件夹,然后放在tomcat根目录下(与conf同一级目录).2.配置conf下的server.xml,修改下面3个节点,如下: & ...
- 对比MFC和Winform及WPF
MFC 生成本机代码,自然是很快.可是,消息循环,减缓了界面显示速度.winform 封装了 win32 的api,多次进行P/invoke 操作 (大部分使用p/invoke操作封装),速度慢 .w ...
- C#模拟登录总结
/// <summary> 登录 /// </summary> /// <param name="url">< ...
- C#中float, double的精度问题
在工作中我发现了一个C#浮点数的精度问题,以下的程序运行结果并未得到我预期的结果: view source print? 01 namespace FloatTest 02 03 class ...