前两篇文章我们写了自定义配置文件,使用@value 加载。这一次我们来看看@ConfigurationProperties 的其他用法
我们都知道ConfigurationProperties 可以声明在java_bean 上面,通过配置文件properties 或者yml 来配置, 在通过配置中心来动态的修改。
这里我们想要自定义一种格式,被ConfigurationProperties 所识别。
这里我们就需要使用到 @ConfigurationPropertiesBinding 这个注解
我们点击进去看源码
/**
* 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";
}
表明了他就作用在配置文件绑定的时候起作用。
这里我们要自定义转换器就要实现
/**
* 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 格式表示。
下面看代码
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}'
测试类:
@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 格式的,
person.dog= {"dogName": "maomao", "dongAge": 3} // 要去掉‘’
person.name= ducheng
也行, 说明我们自定义配置文件转换器成功了。