SpringBoot3读取配置文件application.properties属性值的几种方式
在 Spring Boot 项目中,可以通过以下几种方式读取 application.properties 文件中的配置属性值:
1. 使用 @Value 注解读取单个属性值
@Value 注解可以直接用于注入 application.properties 文件中的属性值。
示例
假设在 application.properties 中定义了以下配置项:
app.name=MyApp app.version=1.0.0
可以在任意 @Component、@Service、@Controller 等注解标注的类中,通过 @Value 注解获取这些值:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppConfig {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
public void printConfig() {
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
注意:
${app.name}表示从配置文件中读取app.name属性值。
2. 使用 @ConfigurationProperties 注解读取配置前缀
@ConfigurationProperties 注解允许批量读取具有相同前缀的属性,并将其注入到一个 Java 类中。这种方式适用于管理大量的配置项。
示例
假设在 application.properties 中定义了一组 app 前缀的属性:
app.name=MyApp app.version=1.0.0 app.description=A sample application
可以创建一个配置类,并使用 @ConfigurationProperties 注解将 app 前缀的属性注入其中:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfigProperties {
private String name;
private String version;
private String description;
// Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
在其他类中可以通过注入 AppConfigProperties 类来获取配置值:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AppService {
private final AppConfigProperties appConfigProperties;
@Autowired
public AppService(AppConfigProperties appConfigProperties) {
this.appConfigProperties = appConfigProperties;
}
public void printAppInfo() {
System.out.println("App Name: " + appConfigProperties.getName());
System.out.println("App Version: " + appConfigProperties.getVersion());
System.out.println("App Description: " + appConfigProperties.getDescription());
}
}
注意:
@ConfigurationProperties注解类必须是一个@Component,并且需要在application.properties中启用属性绑定支持,通常在 Spring Boot 中默认已启用。
3. 使用 Environment 读取属性
Environment 接口提供了动态获取属性值的方式,适合用于在运行时读取配置文件中的属性。
示例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentConfig {
private final Environment environment;
@Autowired
public EnvironmentConfig(Environment environment) {
this.environment = environment;
}
public void printEnvConfig() {
String appName = environment.getProperty("app.name");
String appVersion = environment.getProperty("app.version");
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
注意:
Environment接口适合在需要动态获取配置或配置项不确定的场景中使用。
4. 使用 @PropertySource 加载自定义配置文件
除了 application.properties,也可以定义自定义配置文件,并使用 @PropertySource 注解加载其中的属性。
示例
- 创建自定义配置文件
custom-config.properties,内容如下:
custom.property1=value1 custom.property2=value2
- 使用
@PropertySource注解加载自定义配置文件:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:custom-config.properties")
public class CustomConfig {
@Value("${custom.property1}")
private String property1;
@Value("${custom.property2}")
private String property2;
public void printCustomConfig() {
System.out.println("Custom Property 1: " + property1);
System.out.println("Custom Property 2: " + property2);
}
}
注意:确保
custom-config.properties文件位于classpath下(如src/main/resources目录)。
总结
以上是 Spring Boot 中读取 application.properties 文件属性值的几种常见方式:
- @Value 注解:直接读取单个属性值。
- @ConfigurationProperties 注解:批量读取具有相同前缀的属性。
- Environment 接口:动态读取属性,适合运行时获取。
- @PropertySource 注解:加载自定义配置文件的属性值。
以上就是SpringBoot3读取配置文件application.properties属性值的几种方式的详细内容,更多关于SpringBoot3读取application.properties属性值的资料请关注脚本之家其它相关文章!
相关文章
一文详解SpringBoot使用Kafka如何保证消息不丢失
这篇文章主要为大家详细介绍了SpringBoot使用Kafka如何保证消息不丢失的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以参考一下2025-01-01
关于SpringBoot 打包成的可执行jar不能被其他项目依赖的问题
这篇文章主要介绍了关于SpringBoot 打包成的可执行jar不能被其他项目依赖的问题,本文给大家通过图文实例相结合给大家分享解决方法,需要的朋友可以参考下2020-10-10
idea pom导入net.sf.json的jar包失败的解决方案
JSON(JavaScript Object Notation,JS对象简谱)是一种轻量级的数据交换格式,这篇文章主要介绍了idea pom导入net.sf.json的jar包失败的解决方案,感兴趣的朋友一起看看吧2023-11-11
Java的外部类为什么不能使用private和protected进行修饰的讲解
今天小编就为大家分享一篇关于Java的外部类为什么不能使用private和protected进行修饰的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-04-04
java报错Cause: java.sql.SQLException问题解决
本文主要介绍了java报错Cause: java.sql.SQLException问题解决,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-08-08


最新评论