Springboot-读取核心配置文件及自定义配置文件
读取核心配置文件
核心配置文件是指在resources根目录下的application.properties
或application.yml
配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties
内容如下:
server.port=9090 test.msg=Hello World Springboot!
使用
@Value
方式(常用)
@RestControllerpublic class WebController {@Value("${test.msg}")private String msg;@RequestMapping(value = "index", method = RequestMethod.GET)public String index() {return "The Way 1 : " +msg;}}
注意:在@Value
的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController
表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody
。
访问:http://localhost:9090/index 时将得到The Way 1 : Hello World Springboot!
使用
Environment
方式
@RestController public class WebController {@Autowiredprivate Environment env;@RequestMapping(value = "index2", method = RequestMethod.GET)public String index2() {return "The Way 2 : " + env.getProperty("test.msg");} }
注意:这种方式是依赖注入Evnironment
来完成,在创建的成员变量private Environment env
上加上@Autowired
注解即可完成依赖注入,然后使用env.getProperty("键名")
即可读取出对应的值。
访问:http://localhost:9090/index2 时将得到The Way 2 : Hello World Springboot!
读取自定义配置文件
通过
@ConfigurationProperties
注解,通过getter、setter方法注入及获取配置
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources/config
目录下创建配置文件my-web.properties
resources/config/my-web.properties
内容如下:
web.name=isea533 web.jdbc.username=root web.jdbc.password=root
创建管理配置的实体类:
@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "web") @Component public class MyWebConfig{private String name;private Jdbc jdbc;class Jdbc {private String username;private String password;//getter... }public Integer gePort(){return this.port;}public Jdbc getJdbc() {return this.jdbc;} }
注意:
在
@ConfigurationProperties
注释中有两个属性:locations
:指定配置文件的所在位置prefix
:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以web.
开头)
使用
@Component
是让该类能够在其他地方被依赖使用,即使用@Autowired
注释来创建实例。
通过
@PropertySource
注解,然后使用@Value
逐个注入配置
1 @Configuration 2 @PropertySource("classpath:test.properties") 3 public class ELConfig { 4 5 @Value("${book.name}") 6 private String bookName; 7 8 //PropertySourcesPlaceholderConfigurer这个bean,这个bean主要用于解决@value中使用的${…}占位符。假如你不使用${…}占位符的话,可以不使用这个bean。 9 @Bean 10 public static PropertySourcesPlaceholderConfigurer propertyConfigure() { 11 return new PropertySourcesPlaceholderConfigurer(); 12 } 13 14 public void outputSource() { 15 System.out.println(bookName); 16 } 17 }
转载于:https://www.cnblogs.com/junzi2099/p/7509045.html
Springboot-读取核心配置文件及自定义配置文件相关推荐
- SpringBoot四大核心之starter——自定义starter
四大核心 1.starter:jar包的引入,解决jar版本冲突问题 2.自动装配:简单配置甚至零配置即可运行项目 3.actuator:springboot程序监控器 4.CLI:命令行 start ...
- Java自定义配置文件xml_spring-boot自定义配置文件
环境依赖 sprint-boot version 2.1.7 java version 1.8 pom.xml 依赖jar包 org.springframework.boot spring-boot- ...
- java自定义配置文件_自定义配置文件如何配置
@Component public class RoleDirectiveModel implements TemplateDirectiveModel { /** * * @param enviro ...
- Springboot 之 自定义配置文件及读取配置文件
读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 核心 ...
- 读取文档时出现问题129_springboot读取配置文件的自定义内容时出现中文乱码
学习springboot时读取配置文件自定义内容出现中文乱码总结. 方法一: Windowd→Preferences→General→Editors→Text Editors→Spelling里面的E ...
- java自定义配置文件_基于java读取并引用自定义配置文件
基于java读取并引用自定义配置文件 首先在resources目录创建自定义的配置文件 配置文件的格式: 写工具类,得到配置参数 import java.io.IOException; import ...
- 你只知道@Value?设置server.port就能定义端口号是如何实现的?springboot读取配置文件的多种方式以及源码解析
你只知道@Value?设置server.port就能定义端口号是如何实现的?springboot读取配置文件的多种方式以及原理解析 一.SpringBoot全局配置文件优先级 二.属性注入常用注解 1 ...
- Springboot 读取配置文件参数(yml\properties)
Springboot 读取配置文件的参数,这里介绍2种方式.一种是使用注解@Value,一种是使用属性对应的方式. (说明:我这里贴出来的配置,里面的参数命名是下划线和驼峰命名混用的,这里只是为了演示 ...
- SpringBoot 读取配置文件中参数全面教程
一.简介 在日常开发使用 SpringBoot 框架时,经常有一些配置信息需要放置到配置文件中,我们需要手动读取这些配置到应用中进行一些逻辑,这里整理了一些常用读取配置的方法,简单介绍一下. 1.Sp ...
最新文章
- 深入JVM锁机制2-Lock
- 网络编程--connect()、listen()、accept()
- 基于vue2实现省市联动
- 苹果ipa签名工具免越狱下载_苹果iphone免越狱使用iTunes 一键下载旧版本app
- mdx词典包_推荐几款实用的英语词典
- 用 Python 实现一个大数据搜索引擎
- org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI
- ubutnu 下SVN 提交时忽略某些文件或文件夹
- android update sdk --no-ui,CircleCI Android constraintLayout不起作用
- Python+matplotlib数据可视化鼠标悬停自动标注功能实现
- WebConfig节点详解
- 物联网基础建设-园区智能微电网设计方案
- 东南大学本 硕 博论文中期答辩 毕业答辩ppt模板2021版
- --legacy-peer-deps 作用
- 【预测模型】基于天牛须算法BAS优化BP神经网络实现数据预测matlab代码
- jQuery表单正则校验(邮箱、手机号、身份证)
- python的拼音_Python返回汉字的汉语拼音(原创) | 学步园
- 则必有不完全相同的奇数排列对换为同一个偶排列
- 【郝斌 C语言 笔记】自增
- python的wxpyton库开发的聊天程序
热门文章
- leetcode C++ 25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
- 怎么把一个结构体的数据包通过串口发送出去?
- CTFshow 反序列化 web278
- 通过Matlab发送邮件要注意的问题
- glib中的GList ,GSList,GString,GTree,GTimer
- [YTU]_2476(E3 继承了,成员函数却不可访问)
- Laplacian Eigenmaps 拉普拉斯特征映射
- padarray函数
- This function or variable may be unsafe
- Java锁机制(一)synchronized