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提供了类型安全和结构化的方式来处理配置,因此通常被推荐用于复杂的配置对象。

相关推荐
爱勇宝1 分钟前
《道德经》第 10 章:真正成熟的人,能成事但不控制一切
前端·后端·程序员
六边形66629 分钟前
独立开发不知道做什么?使用 TRAE Work 抓取差评痛点,快速跑通产品立项流
前端·后端·面试
高频因子挖掘机29 分钟前
量化交易系统的数据层和策略层如何解耦?从紧耦合泥潭到优雅分层架构
后端·github
paopaokaka_luck32 分钟前
基于springboot3+vue3的文山民族文化资源展示与推广平台(协同过滤算法、Echarts 图形化分析)
java·前端·spring boot·学习·echarts
临江仙45533 分钟前
同一个 AI Agent 如何同时服务 Web、微信和 QQ:PureChat 的渠道架构实践
前端·人工智能·后端
我的AI队友36 分钟前
DeepSeek Harness 接钉钉通知,踩了两个坑:签名不匹配 + 纯对话刷屏
后端·deepseek
程序员鱼皮37 分钟前
16 个超火的 DeepSeek Harness 插件,大肥鱼已经落后 N 个版本了。。。
前端·后端·ai编程
步行cgn38 分钟前
Spring Cache 详解:Spring 框架的缓存抽象
后端
eralong38 分钟前
Java IO 与 NIO
java·后端