自学内容网 自学内容网

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

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

1. 使用@Value注解

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

application.yml

app:
  name: MySpringBootApp
  version: 1.0.0

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

app:
  name: MySpringBootApp
  version: 1.0.0

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注解:

主类

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类

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

app.name=MySpringBootApp
app.version=1.0.0

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


原文地址:https://blog.csdn.net/qq_42535394/article/details/142714732

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!