网站首页 > 技术文章 正文
Spring Boot 中读取配置文件有以下 5 种方法:
- 使用 @Value 读取配置文件。
- 使用 @ConfigurationProperties 读取配置文件。
- 使用 Environment 读取配置文件。
- 使用 @PropertySource 读取配置文件。
- 使用原生方式读取配置文件。
它们的具体使用方法如下,为了方便测试,我们在 Spring Boot 配置文件 application.properties 添加以下内容:
profile.name=Spring Boot Profile
profile.desc=Spring Boot Profile Desc.
1.使用 @Value 读取配置文件
使用 @Value 可以读取单个配置项,如下代码所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("My Profile Name:" + name);
}
}
以上程序的执行结果如下图所示:
2.使用 @ConfigurationProperties 读取配置文件
@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是读取单个配置项的,而 @ConfigurationProperties 是读取一组配置项的,我们可以使用 @ConfigurationProperties 加实体类读取一组配置项,如下代码所示:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
private String name;
private String desc;
}
其中 prefix 表示读取一组配置项的根 name,相当于 Java 中的类名,最后再把此配置类,注入到某一个类中就可以使用了,如下代码所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Profile profile;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Object:" + profile);
}
}
以上程序的执行结果如下图所示:
3.使用 Environment 读取配置文件
Environment 是 Spring Core 中的一个用于读取配置文件的类,将此类使用 @Autowired 注入到类中就可以使用它的 getProperty 方法来获取某个配置项的值了,如下代码所示:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Name:" + environment.getProperty("profile.name"));
}
}
以上程序的执行结果如下图所示:
4.使用 @PropertySource 读取配置文件
使用 @PropertySource 注解可以用来指定读取某个配置文件,比如指定读取 application.properties 配置文件的配置内容,具体实现代码如下:
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Name:" + name);
}
}
以上程序的执行结果如下图所示:
中文乱码
如果配置文件中出现中文乱码的情况,可通过指定编码格式的方式来解决中文乱码的问题,具体实现如下:
@PropertySource(value = "dev.properties", encoding = "utf-8")
注意事项
@PropertySource 注解默认是只支持 properties 格式配置文件的读取的。
5.使用原生方式读取配置文件
我们还可以使用最原始的方式 Properties 对象来读取配置文件,如下代码所示:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
Properties props = new Properties();
try {
InputStreamReader inputStreamReader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("application.properties"),
StandardCharsets.UTF_8);
props.load(inputStreamReader);
} catch (IOException e1) {
System.out.println(e1);
}
System.out.println("Properties Name:" + props.getProperty("profile.name"));
}
}
以上程序的执行结果如下图所示:
总结
在 Spring Boot 中读取配置文件有以下 5 种方法:
- 使用 @Value 读取配置文件。
- 使用 @ConfigurationProperties 读取配置文件。
- 使用 @PropertySource 读取配置文件。
- 使用 Environment 读取配置文件。
- 使用原生方式读取配置文件。
其中最常用的是前 3 种,如果读取某一个配置项可使用 @Value,如果读取一组配置项可使用 @ConfigurationProperties,如果要指定读取某一个具体的配置文件可使用 @PropertySource 来指定。
猜你喜欢
- 2025-01-03 Java 关键字之 native 详解
- 2025-01-03 三石说:java中常用的几个类
- 2025-01-03 反射、枚举以及Lambda表达式
- 2025-01-03 Java 新手教程,建议收藏
- 2025-01-03 Java零基础入门,科普Java你应该了解什么
- 2025-01-03 Java 代码执行原理
- 2025-01-03 揭秘双亲委派模型:Java类加载的“幕后英雄”
- 2025-01-03 你知道 Java 中关键字 enum 是一个语法糖吗?反编译枚举类
- 2025-01-03 Java中的枚举,这一篇全了,一些不为人知的干货
- 2025-01-03 Java反编译工具
- 1508℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 520℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 490℃MySQL service启动脚本浅析(r12笔记第59天)
- 469℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 467℃启用MySQL查询缓存(mysql8.0查询缓存)
- 447℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 427℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 424℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)