yml自定义属性和值

测试类:

import com.spx.App;
import com.spx.config.MyProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class MyPropertiesTest {

    @Autowired
    private MyProperties properties;

    @Test
    public void test(){
        System.out.println(properties.getArray());
        System.out.println(properties.getList());
        System.out.println(properties.getMap());
        System.out.println(properties.getListMap());
    }
}

### Spring Boot 中从 `application.yml` 文件读取自定义配置的方法 在 Spring Boot 应用程序中,可以通过多种方式从 `application.yml` 文件中读取自定义配置项。以下是几种常见的实现方法及其具体示例。 #### 方法一:使用 `@Value` 注解读取单个属性 通过 `@Value` 注解可以直接注入 YAML 文件中的某个特定键对。例如: ```java import org.springframework.stereotype.Component; @Component public class CustomConfig { @Value("${my.custom.property}") private String customProperty; public String getCustomProperty() { return this.customProperty; } } ``` 上述代码会将 `application.yml` 文件中的 `my.custom.property` 的赋给 `customProperty` 变量[^1]。 --- #### 方法二:使用 `@ConfigurationProperties` 绑定复杂对象 当需要绑定一组相关的配置时,推荐使用 `@ConfigurationProperties` 注解来映射整个结构化的配置数据。这种方式更加灵活且易于维护。 ##### 步骤 1:创建一个 POJO 类表示配置结构 假设 `application.yml` 文件中有以下内容: ```yaml app: name: MyApp version: 1.0.0 settings: timeout: 3000 retries: 5 ``` 可以创建一个 Java 类来匹配该结构: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "app") // 使用前缀 app 来匹配 yml 中的内容 public class AppConfig { private String name; private String version; private Settings settings; // Getter and Setter methods public static class Settings { private int timeout; private int retries; // Getter and Setter methods } } ``` ##### 步骤 2:启用 `@EnableConfigurationProperties` 如果使用的不是默认扫描路径,则需显式启用支持: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties(AppConfig.class) // 显式注册 AppConfig public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 此时即可通过依赖注入的方式获取完整的配置对象[^4]。 --- #### 方法三:结合外部化配置文件 对于某些场景下可能需要动态加载外部资源(如 Nacos 或其他远程服务),则可通过 `bootstrap.yml` 进行初始化设置后再加载本地的 `application.yml` 文件[^3]。例如,在 `bootstrap.yml` 中指定连接地址后,再由应用层调用对应的服务端配置完成最终覆盖逻辑。 --- #### 方法四:手动加载外部属性源 除了自动装配外,还可以利用 `@PropertySource` 定义额外的属性文件位置并将其加入上下文中处理。下面是一个简单的例子展示如何操作: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:custom-config.properties") public class ExternalConfig { @Value("${external.config.value}") private String externalValue; public String getExternalValue() { return this.externalValue; } } ``` 注意这里我们指定了另一个名为 `custom-config.properties` 的文件作为补充输入来源。 --- ### 总结 以上介绍了四种主要途径用于访问存储于 `application.yml` 内部或者关联区域里的个性化设定信息。每种方案各有优劣之处,开发者应依据实际需求选取最合适的策略加以运用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值