0


【Java】读取 springboot 配置文件内容的几种方法

Spring Boot 是一个非常流行的 Java Web 开发框架,它提供了大量的便捷功能和特性,其中读取配置文件是其中的一项重要功能。Spring Boot 支持多种方式读取配置文件,本文将介绍 Spring Boot 读取配置文件的原理和常用方法。

Spring Boot 读取配置文件原理

Spring Boot 读取配置文件的原理是通过 Spring Boot 的自动配置机制实现的。Spring Boot 会根据不同的配置文件类型(如 properties、yml 等)来动态解析配置文件,并将配置文件中的配置项解析到 Spring Boot 配置类中。Spring Boot 配置类可以是一个普通的 Java 类,也可以是一个带有注解的类。配置类中的属性可以使用

@Value

@ConfigurationProperties

注解来注入配置值,从而实现读取配置文件的功能。

Spring Boot 读取配置数据的方法

下面是常用的几种读取 Spring Boot 配置数据的方法。

1. 使用

@Value

注解

@Value

注解可以用来注入配置值,它可以注入常量、系统属性、SpEL 表达式等。使用

@Value

注解读取配置值的示例代码如下:

@Component
public class MyComponent {
    @Value("${my.property}")
    private String myProperty;

    // getter and setter
}

在上面的示例中,

${my.property}

表示从配置文件中读取

my.property

配置项的值,并注入到

myProperty

属性中。

2. 使用

@ConfigurationProperties

注解

@ConfigurationProperties

注解可以用来将配置文件中的配置项映射到一个 Java 类中。使用

@ConfigurationProperties

注解读取配置值的示例代码如下:

@Configuration
@ConfigurationProperties(prefix = "my")
public class MyConfig {
    private String property;

    // getter and setter
}

在上面的示例中,

@ConfigurationProperties(prefix = "my")

表示将以

my

开头的配置项映射到

MyConfig

类中。例如,如果配置文件中有

my.property

配置项,则会将其映射到

property

属性中。

3. 使用

@PropertySource

注解

@PropertySource

注解可以用来指定配置文件的位置。使用

@PropertySource

注解读取配置值的示例代码如下:

@Configuration
@PropertySource("classpath:my.properties")
public class MyConfig {
    @Value("${my.property}")
    private String property;

    // getter and setter
}

在上面的示例中,

@PropertySource("classpath:my.properties")

表示将

classpath

下的

my.properties

文件作为配置文件。

4. 使用

Environment

对象

Environment

对象是 Spring Boot 读取配置文件的核心对象,它可以用来读取配置值、配置文件、系统属性等。使用

Environment

对象读取配置值的示例代码如下:

@Component
public class MyComponent {
    @Autowired
    private Environment env;

    public String getProperty(String key) {
        return env.getProperty(key);
    }
}

在上面的示例中,

env.getProperty(key)

表示从配置文件中读取指定配置项的值。

示例

以下是本文中涉及到的配置文件示例。

1. properties 配置文件示例

application.properties

文件中添加以下配置项:

my.property=Hello, World!

2. yml 配置文件示例

application.yml

文件中添加以下配置项:

my:
  property: Hello, World!

3.

@PropertySource

注解指定的 properties 配置文件示例

my.properties

文件中添加以下配置项:

my.property=Hello, World!

MyConfig

类中使用

@PropertySource

注解指定该文件的位置:

@Configuration
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
public class MyConfig {
    private String property;

    // getter and setter
}

4.

Environment

对象读取配置值示例

application.properties

文件中添加以下配置项:

my.property=Hello, World!

MyComponent

类中使用

Environment

对象读取该配置项的值:

@Component
public class MyComponent {
    @Autowired
    private Environment env;

    public String getProperty() {
        return env.getProperty("my.property");
    }
}

下面是一个完整的、推荐的示例代码:

@Configuration
@ConfigurationProperties(prefix = "my")
@PropertySource("classpath:my.properties")
public class MyConfig {
    private String property;

    @Autowired
    private Environment env;

    // getter and setter

    public String getPropertyFromEnv(String key) {
        return env.getProperty(key);
    }
}

@RestController
public class MyController {
    @Autowired
    private MyConfig myConfig;

    @GetMapping("/property")
    public String getProperty() {
        return myConfig.getProperty();
    }

    @GetMapping("/propertyFromEnv")
    public String getPropertyFromEnv(@RequestParam String key) {
        return myConfig.getPropertyFromEnv(key);
    }
}

在上面的示例中,

MyConfig

类使用了

@ConfigurationProperties

注解和

@PropertySource

注解读取配置文件,并将配置项映射到

property

属性中。

MyController

类中的两个接口分别使用了

MyConfig

类中的两个方法来读取配置值。
希望这些示例可以帮助你更好地理解 Spring Boot 的配置文件读取方式。

总结

Spring Boot 读取配置文件的原理是通过 Spring Boot 的自动配置机制实现的。Spring Boot 支持多种方式读取配置文件,包括使用

@Value

注解、

@ConfigurationProperties

注解、

@PropertySource

注解和

Environment

对象。其中,

@ConfigurationProperties

注解是最常用的方式,它可以将配置文件中的配置项映射到一个 Java 类中,使用起来非常方便。

标签: java spring boot spring

本文转载自: https://blog.csdn.net/m0_47406832/article/details/117289509
版权归原作者 小子宝丁 所有, 如有侵权,请联系我们删除。

“【Java】读取 springboot 配置文件内容的几种方法”的评论:

还没有评论