前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot自定义配置文件转换器

springboot自定义配置文件转换器

作者头像
分享干货的你
发布2021-04-06 16:16:06
1.6K0
发布2021-04-06 16:16:06
举报
文章被收录于专栏:分享干货的你

前两篇文章我们写了自定义配置文件,使用@value 加载。这一次我们来看看@ConfigurationProperties 的其他用法

我们都知道ConfigurationProperties 可以声明在java_bean 上面,通过配置文件properties 或者yml 来配置, 在通过配置中心来动态的修改。

这里我们想要自定义一种格式,被ConfigurationProperties 所识别。

这里我们就需要使用到 @ConfigurationPropertiesBinding 这个注解

我们点击进去看源码

代码语言:javascript
复制
/**
 * Qualifier for beans that are needed to configure the binding of
 * {@link ConfigurationProperties} (e.g. Converters).
 *
 * @author Dave Syer
 */
@Qualifier(ConfigurationPropertiesBinding.VALUE)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationPropertiesBinding {

  /**
   * Concrete value for the {@link Qualifier @Qualifier}.
   */
  String VALUE = "org.springframework.boot.context.properties.ConfigurationPropertiesBinding";

}

表明了他就作用在配置文件绑定的时候起作用。

这里我们要自定义转换器就要实现

代码语言:javascript
复制
/**
 * A converter converts a source object of type {@code S} to a target of type {@code T}.
 *
 * <p>Implementations of this interface are thread-safe and can be shared.
 *
 * <p>Implementations may additionally implement {@link ConditionalConverter}.
 *
 * @author Keith Donald
 * @since 3.0
 * @param <S> the source type
 * @param <T> the target type
 */
@FunctionalInterface
public interface Converter<S, T> {

  /**
   * Convert the source object of type {@code S} to target type {@code T}.
   * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
   * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
   * @throws IllegalArgumentException if the source cannot be converted to the desired target type
   */
  @Nullable
  T convert(S source);

}

s 就是source 源对象, t target 目标对象。写自己的方法实现s 到 T 的转变。

假如我们有一个人,人有了一条狗。 这里我们想把狗的一些特征,在配置文件用 json 格式表示。

下面看代码

代码语言:javascript
复制
package com.ducheng;

import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "person")
public class Person  {
  
  private String name;
  
  private Integer age;
  
    private Dog dog;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  

  public Dog getDog() {
    return dog;
  }

  public void setDog(Dog dog) {
    this.dog = dog;
  }

  
  
  public Person(String name, Integer age, Dog dog) {
    super();
    this.name = name;
    this.age = age;
    this.dog = dog;
  }

  public Person() {
    super();
    // TODO Auto-generated constructor stub
  }  
}


package com.ducheng;
// 记住,方法一定要有set get 
public class Dog {

  private String dogName;
  
  private Integer dongAge;

  public String getDogName() {
    return dogName;
  }

  public void setDogName(String dogName) {
    this.dogName = dogName;
  }

  public Integer getDongAge() {
    return dongAge;
  }

  public void setDongAge(Integer dongAge) {
    this.dongAge = dongAge;
  }

  public Dog(String dogName, Integer dongAge) {
    super();
    this.dogName = dogName;
    this.dongAge = dongAge;
  }

  public Dog() {
    super();
    // TODO Auto-generated constructor stub
  }
  
  
}

@ConfigurationPropertiesBinding
@Component // 必须加上这个,不加上这个不生效
public class CostomConvert implements Converter<String, Dog >{

  @Override
  public Dog convert(String source) {
    // TODO Auto-generated method stub
    return JSON.parseObject(source, Dog.class);
  }



}

yml 配置文件 
person:
  name: ducheng
  dog: '{"dogName": "maomao", "dongAge": 3}'

测试类:

代码语言:javascript
复制
@SpringBootApplication
// 制定开始使用哪个类上面的ConfigurationPropertie
@EnableConfigurationProperties(Person.class)
public class SpringbootEncryptApplication implements ApplicationRunner {
  public static void main(String[] args) {
    SpringApplication.run(SpringbootEncryptApplication.class, args);
  }

  @Override
  public void run(ApplicationArguments args) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("dogName : "+person.getDog().getDogName());
  }

}

我们看启动打印dogname

成功了, 再试一下application.prperties 格式的,

代码语言:javascript
复制
person.dog= {"dogName": "maomao", "dongAge": 3} // 要去掉‘’
person.name= ducheng

也行, 说明我们自定义配置文件转换器成功了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享干货的你 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档