读取配置文件五种方式
目录
- 读取属性配置文件的五种方式
- 读取属性配置的示例
- 属性配置文件
- 方式一:使用注解@Value读取属性配置
- 方式二:使用注解@ConfigurationProperties读取属性配置
- 方式三:使用注解 @PropertySource 和 @Value 来读取属性配置
- 方式四:使用注解 @PropertySource 和 @ConfigurationProperties 来读取属性配置
- 方式五:使用环境变量 Environment 读取属性配置
回到顶部
读取属性配置文件的五种方式
- @Value
- @ConfigurationProperties
- @PropertySource + @Value
- @PropertySource + ConfigurationProperties
- org.springframework.core.env.Environment
回到顶部
读取属性配置的示例
属性配置文件
application.properties
#服务端口号
server.port=9424# redis配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
demo.properties
demo.name=huang
demo.sex=1
demo.type=demo
方式一:使用注解@Value读取属性配置
package com.huang.pims.demo.props;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class ReadByValue {@Value("${server.port}")private int serverPort;@Overridepublic String toString() {return "ReadByValue{" +"serverPort=" + serverPort +'}';}
}
使用此种方式,如无其他需求,可不写setter、getter方法。
方式二:使用注解@ConfigurationProperties读取属性配置
package com.huang.pims.demo.props;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "spring.redis")
public class ReadByConfigurationProperties {private int database;private String host;private String password;private int port;public void setDatabase(int database) {this.database = database;}public void setHost(String host) {this.host = host;}public void setPassword(String password) {this.password = password;}public void setPort(int port) {this.port = port;}public int getDatabase() {return database;}public int getPort() {return port;}public String getHost() {return host;}public String getPassword() {return password;}@Overridepublic String toString() {return "ReadByConfigurationProperties{" +"database=" + database +", host='" + host + '\'' +", password='" + password + '\'' +", port=" + port +'}';}
}
使用此种方式,必须要有成员变量的setter、getter方法。
方式三:使用注解 @PropertySource 和 @Value 来读取属性配置
package com.huang.pims.demo.props;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource(value = {"demo/props/demo.properties"})
public class ReadByPropertySourceAndValue {@Value("${demo.name}")private String name;@Value("${demo.sex}")private int sex;@Value("${demo.type}")private String type;@Overridepublic String toString() {return "ReadByPropertySourceAndValue{" +"name='" + name + '\'' +", sex=" + sex +", type='" + type + '\'' +'}';}
}
注:使用@PropertySource注解读取属性配置,该种方式不支持读取yml配置文件
方式四:使用注解 @PropertySource 和 @ConfigurationProperties 来读取属性配置
package com.huang.pims.demo.props;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource(value = {"demo/props/demo.properties"})
@ConfigurationProperties(prefix = "demo")
public class ReadByPropertySourceAndConfProperties {private String name;private int sex;private String type;public void setName(String name) {this.name = name;}public void setSex(int sex) {this.sex = sex;}public void setType(String type) {this.type = type;}public String getName() {return name;}public int getSex() {return sex;}public String getType() {return type;}@Overridepublic String toString() {return "ReadByPropertySourceAndConfProperties{" +"name='" + name + '\'' +", sex=" + sex +", type='" + type + '\'' +'}';}
}
方式五:使用环境变量 Environment 读取属性配置
package com.huang.pims.demo.props;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;@Component
public class ReadByEnv {@Autowiredprivate Environment environment;public String getServerPort() {return environment.getProperty("server.port");}}
测试类
package com.huang.pims.demo.runners;import com.huang.pims.demo.props.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class OutputPropsRunner implements CommandLineRunner {private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class);@Autowiredprivate ReadByValue readByValue;@Autowiredprivate ReadByConfigurationProperties readByConfigurationProperties;@Autowiredprivate ReadByPropertySourceAndValue readByPropertySourceAndValue;@Autowiredprivate ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties;@Autowiredprivate ReadByEnv readByEnv;@Overridepublic void run(String... args) throws Exception {LOGGER.info(readByValue.toString());LOGGER.info(readByConfigurationProperties.toString());LOGGER.info(readByPropertySourceAndValue.toString());LOGGER.info(readByPropertySourceAndConfProperties.toString());LOGGER.info(readByEnv.getServerPort());}}
测试方法,启动项目即可看到效果。
从截图中可以看出,需要读取的属性配置,都已经成功读取出来了。
分类: Spring Boot
标签: Spring Boot, 属性配置, @Value, @ConfigurationProperties, @PropertySource, Environment
读取配置文件五种方式相关推荐
- java中读取properties文件内容五种方式
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- 五种方式让你在java中读取properties文件内容不再是难题
2019独角兽企业重金招聘Python工程师标准>>> 方式1.通过context:property-placeholder加载配置文件jdbc.properties中的内容 < ...
- php怎么读取txt文件_PHP读取文件内容的五种方式
php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp); -- php读取文件内容: -----第一种方法 ...
- SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式
这对时间在学习SSH中Spring架构,Spring的事务配置做了具体总结.在此之间对Spring的事务配置仅仅是停留在听说的阶段,总结一下.总体把控.通过这次的学习发觉Spring的事务配置仅仅要把 ...
- Spring事务配置的五种方式 说明
Spring事务配置的五种方式 [转 http://blog.csdn.net/hjm4702192/article/details/17277669] Spring配置文件中关于事务配置总是由三个 ...
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别、不可重复读与幻读的区别
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spr ...
- JDBC连接Mysql的五种方式
测试环境说明 mysql数据库:jdbc:mysql://localhost:3306/test IDE:IDEA 2022 JDK:JDK8 mysql:mysql 5.7 JDBC:5.1.37 ...
- 【JDBC篇】Java连接数据库的五种方式,及什么是URL?
目录 URL介绍: 连接数据库的五种方式 方式一: 方式二: 方式三: 方式四: 方式五:(最终版) URL介绍: JDBC URL 用于标识一个被注册的驱动程序,驱动程序管理器通过这个 URL 选择 ...
- idea连接数据库五种方式
连接数据库的五种方式 方式一 public void testConnection() throws SQLException {Driver driver=new com.mysql.cj.jdbc ...
- 数据库连接的五种方式讲解
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 JDBC:获取数据库连接的5种方式 获取数据库连接前,我们需要做一些前期准备: 在项目工程中导入数据库连接相关数据库驱动(也就是第三方 ...
最新文章
- 不一样的命令行 – Windows PowerShell简介
- HLS视频协议第一弹--centos下面配置ffmpeg,segmenter以适应hls切片需要
- UDP:用戶数据报协议(读书笔记之二)
- 解决Extjs中textarea不支持keyup事件的问题
- C ++ 指针 | this指针_2
- mysql中gtid关闭方法_CDH-mysql 开启关闭 gtid
- 对CMSIS的学习(第1-3部分)
- wav用matlab打不开,Matlab如何读取wav文件,为什么wavread不行啊
- HEAD detached at ---
- 重学React基础知识整理——组件间的另类通信“插槽”(五)
- Chrome浏览器输入不安全站点,没有继续前往链接--解决方案thisisunsafe
- Amplify Shader Editor 案例解析系列——(1)2Sided
- 【转】[Qt教程] Qt串口通信全新专题
- python爬虫模拟有道翻译
- 基于SC-LIO-SAM的SLAM实践
- 求学者们论文的引用次数(中等难度C++)
- matlab中的addemup是什么,毕业论文-rsa密码体制的设计及matlab语言下的实现
- Android必知必会-自定义Scrollbar样式
- 送30本曹大的《你凭什么做好互联网》
- Automatic differentiation
热门文章
- Web安全实践(12)密码探测
- 猝不及防,iOS9.3测试版已经遭到越狱?
- android studio执行 Information:Gradle tasks [:app:assembleDebug]失败处理
- 互联网+传统硬件,乐视与酷派要构建全新生态链?
- GoldenGate新增表
- 原创:爱是两个人的事
- 【Android每日一讲】2012.11.08 Android 多语系支持 -- Locale与Configuration
- linux 用户与工作组
- [藏]常用的匹配正则表达式和实例
- KVM 介绍(3):I/O 全虚拟化和准虚拟化 [KVM I/O QEMU Full-Virtualizaiton Para-virtualization]