Springboot读取配置文件及自定义配置文件

Easter79
• 阅读 1154

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

Springboot读取配置文件及自定义配置文件

1   <parent>
 2     <groupId>org.springframework.boot</groupId>
 3     <artifactId>spring-boot-starter-parent</artifactId>
 4     <version>1.5.9.RELEASE</version>
 5     </parent>
 6 
 7   <dependencies>
 8     <dependency>
 9         <groupId>org.springframework.boot</groupId>
10         <artifactId>spring-boot-starter-web</artifactId>
11     </dependency>
12     <!-- 单元测试使用 -->
13     <dependency>
14         <groupId>org.springframework.boot</groupId>
15         <artifactId>spring-boot-starter-test</artifactId>
16     </dependency>
17   
18     <dependency>
19       <groupId>junit</groupId>
20       <artifactId>junit</artifactId>
21       <scope>test</scope>
22     </dependency>
23    
24   </dependencies>

Springboot读取配置文件及自定义配置文件

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

Springboot读取配置文件及自定义配置文件

1 package com.kelly.controller;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.context.annotation.ComponentScan;
 6 import org.springframework.context.annotation.Configuration;
 7 
 8 @Configuration
 9 @EnableAutoConfiguration //自动加载配置信息
10 @ComponentScan("com.kelly")//使包路径下带有注解的类可以使用@Autowired自动注入
11 public class StartApplication {
12     public static void main(String[] args) {
13         SpringApplication.run(StartApplication.class, args);
14     }
15 }

Springboot读取配置文件及自定义配置文件

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

  application.properties

Springboot读取配置文件及自定义配置文件

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

test.name=kelly
test.password=admin123

Springboot读取配置文件及自定义配置文件

  define.properties

defineTest.pname=test
defineTest.password=test123

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

FirstController.java

Springboot读取配置文件及自定义配置文件

1 package com.kelly.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 
 9 @Controller
10 public class FirstController {
11     
12     @Value("${test.name}")
13     private String name;
14     
15     @Value("${test.password}")
16     private String password;
17     
18     @RequestMapping("/")
19     @ResponseBody
20     String home()
21     {
22         return "Hello Springboot!";
23     }
24     
25     @RequestMapping("/hello")
26     @ResponseBody
27     String hello()
28     {
29         return "name: " + name + ", " + "password: " + password;
30     }
31 }

Springboot读取配置文件及自定义配置文件

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

Springboot读取配置文件及自定义配置文件

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

DefineEntity.java

Springboot读取配置文件及自定义配置文件

1 package com.kelly.entity;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.context.annotation.PropertySource;
 5 import org.springframework.stereotype.Component;
 6 
 7 @Component
 8 @ConfigurationProperties(prefix="defineTest")
 9 @PropertySource("classpath:define.properties")
10 public class DefineEntity {
11     
12     private String pname;
13     
14     private String password;
15 
16     public String getPname() {
17         return pname;
18     }
19 
20     public void setPname(String pname) {
21         this.pname = pname;
22     }
23 
24     public String getPassword() {
25         return password;
26     }
27 
28     public void setPassword(String password) {
29         this.password = password;
30     }
31     
32     
33 }

Springboot读取配置文件及自定义配置文件

SecondController.java

Springboot读取配置文件及自定义配置文件

1 package com.kelly.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 import com.kelly.entity.DefineEntity;
 9 
10 @Controller
11 public class SecondController {
12     
13     @Autowired
14     DefineEntity defineEntity;
15     
16     @RequestMapping("/define")
17     @ResponseBody
18     String define()
19     {
20         return "test.name:" + defineEntity.getPname() + ", test.password:" + defineEntity.getPassword();
21     }
22 }

Springboot读取配置文件及自定义配置文件

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

Springboot读取配置文件及自定义配置文件

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

Springboot读取配置文件及自定义配置文件

转自:https://www.cnblogs.com/kellyJAVA/p/8030395.html

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
2星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Souleigh ✨ Souleigh ✨
3年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
6个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k