springboot 代码读取读取yml文件

配置config 

package com.ljzforum.platform.config;

import com.ljzforum.platform.util.CustomizedPropertyConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.Properties;
@Configuration
public class YmlConfig {
    @Autowired
    private Environment env;

    @Bean
    public CustomizedPropertyConfigurer ymlConfigurer() {
        System.out.println(env.getActiveProfiles()[0]);
        //1:加载配置文件
        Resource app = new ClassPathResource("application.yml");
        Resource appEnv = new ClassPathResource("application-"+env.getActiveProfiles()[0]+".yml");
        Resource oss = new ClassPathResource("oss.yml");
        Resource message = new ClassPathResource("message.yml");
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        // 2:将加载的配置文件交给 YamlPropertiesFactoryBean
        yamlPropertiesFactoryBean.setResources(app, oss, message, appEnv);
        // 3:将yml转换成 key:val
        Properties properties = yamlPropertiesFactoryBean.getObject();
        // 4: 将Properties 通过构造方法交给我们写的工具类
        CustomizedPropertyConfigurer ymlConfigurer = new CustomizedPropertyConfigurer(properties);
        return ymlConfigurer;
    }
}

 

CustomizedPropertyConfigurer


import java.util.Properties;

public class CustomizedPropertyConfigurer {

	private static Properties ymlProperties = new Properties();

	public CustomizedPropertyConfigurer(Properties properties){
		ymlProperties = properties;
	}

	public static Object getContextProperty(String key) {
		return ymlProperties.getProperty(key);
	}
	
	public static Object setContextProperty(String key,Object value) {
		return ymlProperties.setProperty(key, value.toString());
	}

}

读取

String name = (String) CustomizedPropertyConfigurer.getContextProperty("NAME");

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 项目中,读取 YAML 配置文件的方式与读取 properties 配置文件的方式类似。可以使用 `@ConfigurationProperties` 注解将配置文件中的属性与 Java 对象绑定起来,也可以使用 `@Value` 注解逐个读取配置。 以下是使用 `@ConfigurationProperties` 注解读取 YAML 配置文件的示例代码: 1. 创建一个 Java 类,用于存储 YAML 配置文件中的属性: ```java @ConfigurationProperties(prefix = "example") public class ExampleProperties { private String name; private int age; //其他属性... //getter和setter方法... } ``` 2. 在 YAML 配置文件中定义属性: ```yaml example: name: "Tom" age: 18 #其他属性... ``` 3. 在 Spring Boot 应用程序的配置类中,启用 `@EnableConfigurationProperties` 注解,并将 `ExampleProperties` 类加入到 Spring 容器中: ```java @EnableConfigurationProperties(ExampleProperties.class) @Configuration public class AppConfig { @Bean public ExampleProperties exampleProperties() { return new ExampleProperties(); } } ``` 4. 在需要读取配置的地方,注入 `ExampleProperties` 对象即可使用: ```java @RestController public class ExampleController { @Autowired private ExampleProperties exampleProperties; @GetMapping("/example") public ExampleProperties getExample() { return exampleProperties; } } ``` 以上示例代码使用了 `@ConfigurationProperties` 注解将 YAML 配置文件中的属性与 `ExampleProperties` 类绑定起来,并在 Spring Boot 应用程序的配置类中将 `ExampleProperties` 类加入到 Spring 容器中。在需要读取配置的地方,使用 `@Autowired` 注解注入 `ExampleProperties` 对象即可使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值