SpringBoot读取resources下的自定义yaml文件(配置只需两步)

SpringBoot读取resources下的自定义yaml文件


permission.yml文件内容:

uri:

  # 数组
  admin:
    - /v1/mytoken/picture
    - /v1/
    
  # string字符串
  test: test
  1. 通过PropertySourcePlaceholderConfigurer来加载yml文件,暴露yml文件到spring environment
// 加载YML格式自定义配置文件
	@Bean
	public static PropertySourcesPlaceholderConfigurer properties() {
		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//		yaml.setResources(new FileSystemResource(ResourceUtils.CLASSPATH_URL_PREFIX + "permission.yml"));//File引入
		yaml.setResources(new ClassPathResource("permission.yml"));//class引入,避免了路径处理问题
		configurer.setProperties(yaml.getObject());
		return configurer;
	}

注:这块代码可以直接放在启动类下,或者新建一个类配合@Configuration使用,@Bean的使用方法可以参考我之前写的博客:Springboot之@Bean和@Configuration的简单使用

  1. 编写一个model与yaml的映射,可以直接通过 类.属性 的方式去调用yaml
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@Data
@Component
@ConfigurationProperties(prefix = "uri")
public class Permission {

   // 这里不需要 @Value
   private List<String> admin;
   private String test;

}

配置完毕,以下为调用及测试内容:

  1. 通过注入的方式调用
import com.eyuai.eyuaiCMS.model.common.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

   @Autowired
   private Permission permission;

   @GetMapping("/test")
   public Permission test(){
       return permission;
   }
}
  1. 测试结果:前面加了 “-”的是数组,可以直接用list接收。
    测试
    项目结构:
    项目结构
    更多解释可以参考这里:一篇写的很通俗易懂的博客
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目,可以通过在`application.properties`或`application.yml`配置文件定义配置属性,然后在项目启动时使用`@Value`注解注入属性值来读取配置文件数据。 以`application.properties`为例,首先在`src/main/resources`目录下创建该文件,并定义读取配置属性,例如: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=password ``` 然后在要使用该配置属性的类使用`@Value`注解注入属性值,例如: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // getters and setters } ``` 在上面的代码,通过在`@Value`注解使用`${}`来引用配置属性,然后将属性值注入到对应的变量要注意的是,使用`@Value`注解注入属性值的类必须是Spring Bean,因此要在类上添加`@Component`注解或其它符合条件的注解,以便Spring能够扫描并创建该类的实例。同时,要在Spring Boot应用程序的入口类上添加`@EnableConfigurationProperties`注解,以启用注入属性值的功能。 另外,如果读取`application.yml`配置文件的属性值,可以使用类似的方式,并在`@Value`注解使用`:`来引用属性,例如: ```yaml # 数据库连接配置 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: password ``` ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DatabaseConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; // getters and setters } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值