Spring Boot中获取application.yml中属性的几种方式

在Spring Boot应用程序中,可以通过多种方式从application.yml文件中获取配置属性。以下是几种常见的方法:

1. 使用@Value注解

你可以使用@Value注解将application.yml中的属性注入到Spring管理的bean中。

application.yml

yaml 复制代码
app:
  name: MySpringBootApp
  version: 1.0.0

Java类

java 复制代码
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;

    // Getter and Setter methods
    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }
}

2. 使用@ConfigurationProperties注解

@ConfigurationProperties注解提供了一种类型安全的方式来绑定配置属性到Java对象。

application.yml

yaml 复制代码
app:
  name: MySpringBootApp
  version: 1.0.0

Java类

java 复制代码
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {

    private String name;
    private String version;

    // 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;
    }
}

你还需要在Spring Boot的主类或者配置类上启用@EnableConfigurationProperties注解:

主类

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(AppConfig.class)
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

3. 使用Environment对象

你可以通过注入Environment对象来获取配置属性。

Java类

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class AppConfig {

    @Autowired
    private Environment env;

    public String getAppName() {
        return env.getProperty("app.name");
    }

    public String getAppVersion() {
        return env.getProperty("app.version");
    }
}

4. 使用@PropertySource注解(不推荐用于YAML文件)

@PropertySource注解通常用于加载.properties文件,而不是.yml文件。但如果你坚持使用.properties文件,可以这样:

application.properties

properties 复制代码
app.name=MySpringBootApp
app.version=1.0.0

Java类

java 复制代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:application.properties")
public class AppConfig {

    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    // Getter and Setter methods
    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }
}

注意:对于YAML文件,通常使用前面提到的@ConfigurationProperties或@Value注解。

选择哪种方法取决于你的具体需求和偏好。@ConfigurationProperties提供了类型安全和结构化的方式来处理配置,因此通常被推荐用于复杂的配置对象。

相关推荐
const_qiu17 分钟前
第四章 00:开篇 —— 从「会聊天」到「记得住、会干活、能协作」
java·开发语言·人工智能·python
Wang's Blog23 分钟前
Java 项目实战: 交友项目 - 个人资料管理的更新接口实现与坑
java·开发语言·交友
团子哒哒24 分钟前
数组、切片和Map
后端
Promising_GEO27 分钟前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI
java·sentinel·遥感·空间分析
专注仿真32 分钟前
ThinkPHP6 API接口开发实例:从零搭建RESTful风格接口
后端·php·restful
大文说跨境33 分钟前
亚马逊多账号管理浏览器怎么选?云登/紫鸟/AdsPower 对比
java·前端·自动化
开心猴爷1 小时前
应对iOS审核4.3:可以使用 IpaGuard 来给属性名混淆的技术细节与平衡之道
后端·ios
1m555555551 小时前
RAG 工程实战(五):正确知识已经进入 Prompt,为什么大模型还是会答错?
后端·面试
CoderYanger1 小时前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
易小尊1 小时前
我的模型在 65% 以上时准得可怕,在 50% 时像在掷硬币
后端