自定义springboot yaml配置

自定义springboot yml配置

前言

在平时使用Springboot的时候,利用springboot或者三方jar包,都能轻易的在yml文件中实现配置。如何实现自己的yml配置呢?

实现

引入pom依赖

   <!--自定义yml需要引入的依赖-->
   <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
		</dependency>
     <!--使用Lombok,注意:这里Lombok是可选的-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

新建元数据JSON格式文件

  • 在resource文件夹下新建META-INF文件夹
  • 在META-INF文件夹下新建spring-configuration-metadata.json文件

文件内容如下:

{
  "groups": [
    {
    "name": "jack.sparrow",
    "type": "com.learn.spring.spring01.config.CustomConfigurationProperties",
    "sourceType": "com.learn.spring.spring01.config.CustomConfigurationProperties"
  }
  ],
  "properties": [
    {
     "name": "jack.sparrow.enabled",
      "type": "java.lang.Boolean",
      "defaultValue": true,
      "description": "是否是配置文件生效"
    },
    {
    "name": "jack.sparrow.code",
    "type": "java.lang.Integer",
    "sourceType": "com.learn.spring.spring01.config.CustomConfigurationProperties",
    "description": "这是property的类的code",
    "defaultValue": 1
    },
    {
      "name": "jack.sparrow.name",
      "type": "java.lang.String",
      "sourceType": "com.learn.spring.spring01.config.CustomConfigurationProperties"
    },
    {
      "name": "jack.sparrow.check",
      "type": "java.lang.Boolean",
      "sourceType": "com.learn.spring.spring01.config.CustomConfigurationProperties",
      "description": "这是check",
      "defaultValue": true
    },
    {
      "name": "jack.sparrow.name.nameMap",
      "type": "java.util.Map<java.lang.String,java.lang.String>",
      "sourceType": "com.learn.spring.spring01.config.CustomConfigurationComplexProperties",
      "description": "这是map"
    }
  ]
}

groups:里面的内容是自定义配置项的父节点,有多少个父节点就配置多少个groups.

group里的name是配置yml的父节点名字

group里的type、sourceType是一样的,对应的都是整个配置项对应的配置类的去全路径

properties:里面的内容是每个父节点下具体的子节点项,就是yml文件里具体设置值的地方

properties里的name是配置yml的子节点名字

properties里的type,是子节点对应配置类里的属性的数据类型,如java.lang.String等

prooerties里的description是对该节点配置项的描述

properties里的defaultValue是该节点不配置时的默认值

properties里的sourceType对应的配置类的全路径

新建CustomConfigurationProperties配置类

@Getter
@Setter
@ConfigurationProperties(prefix = "jack.sparrow")
public class CustomConfigurationProperties {

    // private String name;
    private Integer code;
    private Boolean check;

}

新建CustomConfigurationComplexProperties配置类

@Getter
@Setter
@ConfigurationProperties(prefix = "jack.sparrow.name")
public class CustomConfigurationComplexProperties {

    private Map<String,String> nameMap = new HashMap<>();
}

说明:在上面的JSON文件中,name这个二级子节点是不设置值的,nameMap这个三级子节点是需要设置值的,这就需要两个配置类了。因为prefix只能定位到prefix的下一层深度,再深是无法定位到的

启动类配置

@EnableConfigurationProperties({CustomConfigurationProperties.class, CustomConfigurationComplexProperties.class})
@SpringBootApplication
public class Spring01Application {

	public static void main(String[] args) {
		SpringApplication.run(Spring01Application.class, args);
	}
}

在yml文件中配置

jack:
  sparrow:
    check: false
    name:
      nameMap:
           jack: "this is jack"
           rose: "this is rose"
    code: 3

如图所示:
yaml提示

测试配置文件是否生效

@Configuration
@ConditionalOnProperty(
        prefix = "jack.sparrow",
        name = {"enabled"},
        havingValue = "true",
        matchIfMissing = true
)
@AllArgsConstructor
public class CustomConfiguration implements EnvironmentAware {
    private final CustomConfigurationProperties properties;
    private final CustomConfigurationComplexProperties complexProperties;

    @Override
    public void setEnvironment(Environment environment) {
        String prefix = "jack.sparrow.";
        StandardEnvironment standardEnvironment = (StandardEnvironment) environment;
        this.getProValue(prefix,standardEnvironment);
    }


    private void getProValue(String prefix,Environment environment){
        String code = environment.getProperty(prefix + "code");
        System.out.println(code);
        Map<String, String> nameMap = complexProperties.getNameMap();
        nameMap.forEach((k,v) -> {
            System.out.println(k);
            System.out.println(v);
        });
        System.out.println(properties.getCheck());
    }
}

启动程序,观察控制台,即可看到打印出的配置的内容

name(json文件中配置的某一项属性(properties)的name) 和 havingValue配合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置

matchIfMissing,缺少该property时是否可以加载。如果为true,没有该property也会正常加载;反之报错

自定义自动配置类

有了自定义的yml,接下来,如何在外部应用引入我们的依赖之后在某些条件下自动配置呢?如阿里的Druid数据源、Feign框架等等很多框架,当提供了spring-boot-starter之后,引入依赖之后都会进行自动配置。如何自定义自动配置类呢?
Spring Boot关于自动配置的官方文档,里面很详细的说明了,如何实现自动配置,从自定义,到测试自动配置是否生效
示例代码。博主本人的自定义的自动配置详见这里

参考

Spring官方问答关于元数据配置部分

思否文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值