在SpringBoot下怎么读取自定义properties配置文件
来源:亿速云
时间:2024-04-09 16:27:11 106浏览 收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《在SpringBoot下怎么读取自定义properties配置文件》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
一、在resource中新建.properties文件
在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下。如图remote.properties所示
二、编写配置文件
remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource/pic/
三、新建一个配置类RemoteProperties.java
@Configuration @ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) @PropertySource("classpath:config/remote.properties") @Data @Component public class RemoteProperties { private String uploadFilesUrl; private String uploadPicUrl; }
其中
@Configuration 表明这是一个配置类
@ConfigurationProperties(prefix = "remote", ignoreUnknownFields = false) 该注解用于绑定属性。prefix用来选择属性的前缀,也就是在remote.properties文件中的“remote”,ignoreUnknownFields是用来告诉SpringBoot在有属性不能匹配到声明的域时抛出异常。
@PropertySource("classpath:config/remote.properties") 配置文件路径
@Data 这个是一个lombok注解,用于生成getter&setter方法
@Component 标识为Bean
四、如何使用?
在想要使用配置文件的方法所在类上表上注解EnableConfigurationProperties(RemoteProperties.class)
并自动注入
@Autowired RemoteProperties remoteProperties;
在方法中使用 remoteProperties.getUploadFilesUrl()就可以拿到配置内容了。
@EnableConfigurationProperties(RemoteProperties.class) @RestController public class TestService{ @Autowired RemoteProperties remoteProperties; public void test(){ String str = remoteProperties.getUploadFilesUrl(); System.out.println(str); } }
这里str就是配置文件中的”/resource/files/”了。
PS:下面看下 Spring-boot中读取config配置文件的两种方式
了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件的信息。
Spring-Boot读取配置文件的方式:
一.读取核心配置文件信息application.properties的内容
核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties内容如下:
test.msg=Hello World SpringBoot
方式一:使用@Value方式(常用)
package Solin.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WebController { @Value("${test.msg}") private String msg; @RequestMapping("/index1") public String index1(){ return "方式一:"+msg; } }
注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:8088/index1时得到:"方式一:Hello World SpringBoot"
方式二:使用Environment方式
package Solin.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WebController { @Autowired private Environment env; @RequestMapping("/index2") public String index2(){ return "方式二:"+env.getProperty("test.msg"); } }
注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。
访问:http://localhost:8088/index2时得到:"方式二:Hello World SpringBoot"
二.读取自定义配置文件信息,例如:author.properties
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件author.properties
resources/author.properties内容如下:
author.name=Solin author.age=22
创建管理配置的实体类:
package Solin.controller; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; //加上注释@Component,可以直接在其他地方使用@Autowired来创建其实例对象 @Component @ConfigurationProperties(prefix = "author",locations = "classpath:author.properties") public class MyWebConfig{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
注意:
在@ConfigurationProperties注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以author.开头)
使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。
创建测试Controller
package Solin.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; @Controller public class ConfigController { @Autowired private MyWebConfig conf; @RequestMapping("/test") public @ResponseBody String test() { return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge(); } }
注意:由于在Conf类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。
访问:http://localhost:8088/test时得到:"Name:Solin---Age:22"
今天关于《在SpringBoot下怎么读取自定义properties配置文件》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于SpringBoot,Properties的内容请关注golang学习网公众号!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
278 收藏
-
236 收藏
-
237 收藏
-
194 收藏
-
269 收藏
-
124 收藏
-
114 收藏
-
214 收藏
-
166 收藏
-
287 收藏
-
465 收藏
-
493 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习