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

相关推荐
狐凄24 分钟前
Python实例题:使用Pvthon3编写系列实用脚本
java·网络·python
Lxinccode2 小时前
Java查询数据库表信息导出Word-获取数据库实现[1]:KingbaseES
java·数据库·word·获取数据库信息·获取kingbasees信息
元亓亓亓3 小时前
Java后端开发day36--源码解析:HashMap
java·开发语言·数据结构
sd21315123 小时前
RabbitMQ 复习总结
java·rabbitmq
他҈姓҈林҈3 小时前
使用 Spring Boot 进行开发
spring boot
码银5 小时前
Java 集合:泛型、Set 集合及其实现类详解
java·开发语言
东阳马生架构5 小时前
Nacos简介—4.Nacos架构和原理
java
柏油6 小时前
MySQL InnoDB 行锁
数据库·后端·mysql
咖啡调调。6 小时前
使用Django框架表单
后端·python·django
Java&Develop6 小时前
onloyoffice历史版本功能实现,版本恢复功能,编辑器功能实现 springboot+vue2
前端·spring boot·编辑器