I need transparent convert from array in application.yml to specific type in my app. I want use
@ConfigurationPropertiesBinding, but converter (SimpleConverter) work only for simple source types. Why does analog (ComplexConverter) not work for complex source type?
Standalone example:
package com.example;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.util.List;
@SpringBootTest
@SpringBootConfiguration
@ComponentScan("com.example")
@EnableConfigurationProperties(ConfBean.class)
public class ComplexConverterTest {
@Autowired
private ConfBean confBean;
@Test
public void test() {
Assertions.assertEquals(2 * 2, confBean.getSimple().getPowValue()); //work
//after I take NullPointer
Assertions.assertEquals(2, confBean.getComplex().getLeft());
Assertions.assertEquals(3, confBean.getComplex().getLeft());
}
}
class SimpleVal {
private final int powValue;
SimpleVal(int powValue) {
this.powValue = powValue;
}
public int getPowValue() {
return powValue;
}
}
@Component
@ConfigurationPropertiesBinding
class SimpleConverter implements Converter<Integer, SimpleVal> {
@Override
public SimpleVal convert(Integer source) {
return new SimpleVal(source * source);
}
}
@Component
@ConfigurationPropertiesBinding
class ComplexConverter implements Converter<List<Integer>, ComplexVal> {
@Override
public ComplexVal convert(List<Integer> source) {
return new ComplexVal(source.get(0), source.get(1));
}
}
class ComplexVal {
private final int left;
private final int right;
ComplexVal(int left, int right) {
this.left = left;
this.right = right;
}
public int getLeft() {
return left;
}
public int getRight() {
return right;
}
}
@ConfigurationProperties("conf")
class ConfBean {
private SimpleVal simple;
private ComplexVal complex;
public SimpleVal getSimple() {
return simple;
}
public void setSimple(SimpleVal simple) {
this.simple = simple;
}
public ComplexVal getComplex() {
return complex;
}
public void setComplex(ComplexVal complex) {
this.complex = complex;
}
}
It is compilable/executable.
My application.yaml
conf:
simple: 2
complex: [ 2, 3 ]
Thanks for attention.