Commit c23af8f5 authored by Stephane Nicoll's avatar Stephane Nicoll

Support of GenericConverter

This commit makes sure to also include any `GenericConverter` flagged for
inclusion in the conversion service to use to convert configuration keys.

Previously, those were ignored as `Converter` and `GenericConverter` are
two separate interfaces.

Closes gh-4988
parent 6f8d4c77
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -47,6 +47,7 @@ import org.springframework.core.PriorityOrdered; ...@@ -47,6 +47,7 @@ import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
...@@ -108,6 +109,8 @@ public class ConfigurationPropertiesBindingPostProcessor ...@@ -108,6 +109,8 @@ public class ConfigurationPropertiesBindingPostProcessor
private List<Converter<?, ?>> converters = Collections.emptyList(); private List<Converter<?, ?>> converters = Collections.emptyList();
private List<GenericConverter> genericConverters = Collections.emptyList();
private int order = Ordered.HIGHEST_PRECEDENCE + 1; private int order = Ordered.HIGHEST_PRECEDENCE + 1;
/** /**
...@@ -121,6 +124,17 @@ public class ConfigurationPropertiesBindingPostProcessor ...@@ -121,6 +124,17 @@ public class ConfigurationPropertiesBindingPostProcessor
this.converters = converters; this.converters = converters;
} }
/**
* A list of custom converters (in addition to the defaults) to use when converting
* properties for binding.
* @param converters the converters to set
*/
@Autowired(required = false)
@ConfigurationPropertiesBinding
public void setGenericConverters(List<GenericConverter> converters) {
this.genericConverters = converters;
}
/** /**
* Set the order of the bean. * Set the order of the bean.
* @param order the order * @param order the order
...@@ -407,6 +421,9 @@ public class ConfigurationPropertiesBindingPostProcessor ...@@ -407,6 +421,9 @@ public class ConfigurationPropertiesBindingPostProcessor
for (Converter<?, ?> converter : this.converters) { for (Converter<?, ?> converter : this.converters) {
conversionService.addConverter(converter); conversionService.addConverter(converter);
} }
for (GenericConverter genericConverter : this.genericConverters) {
conversionService.addConverter(genericConverter);
}
this.defaultConversionService = conversionService; this.defaultConversionService = conversionService;
} }
return this.defaultConversionService; return this.defaultConversionService;
......
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package org.springframework.boot.bind; package org.springframework.boot.bind;
import java.util.Collections;
import java.util.Set;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -30,7 +33,9 @@ import org.springframework.boot.test.SpringApplicationConfiguration; ...@@ -30,7 +33,9 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
...@@ -40,21 +45,26 @@ import static org.junit.Assert.assertThat; ...@@ -40,21 +45,26 @@ import static org.junit.Assert.assertThat;
* Tests for {@link ConfigurationProperties} binding with custom converters. * Tests for {@link ConfigurationProperties} binding with custom converters.
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(TestConfig.class) @SpringApplicationConfiguration(TestConfig.class)
@IntegrationTest("foo=bar") @IntegrationTest({"foo=one", "bar=two"})
public class ConverterBindingTests { public class ConverterBindingTests {
@Value("${foo:}") @Value("${foo:}")
private String foo; private String foo;
@Value("${bar:}")
private String bar;
@Autowired @Autowired
private Wrapper properties; private Wrapper properties;
@Test @Test
public void overridingOfPropertiesOrderOfAtPropertySources() { public void overridingOfPropertiesOrderOfAtPropertySources() {
assertThat(this.properties.getFoo().getName(), is(this.foo)); assertThat(this.properties.getFoo().name, is(this.foo));
assertThat(this.properties.getBar().name, is(this.bar));
} }
@Configuration @Configuration
...@@ -68,9 +78,22 @@ public class ConverterBindingTests { ...@@ -68,9 +78,22 @@ public class ConverterBindingTests {
@Override @Override
public Foo convert(String source) { public Foo convert(String source) {
Foo foo = new Foo(); return new Foo(source);
foo.setName(source); }
return foo; };
}
@Bean
public GenericConverter genericConverter() {
return new GenericConverter() {
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, Bar.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return new Bar((String) source);
} }
}; };
} }
...@@ -84,16 +107,20 @@ public class ConverterBindingTests { ...@@ -84,16 +107,20 @@ public class ConverterBindingTests {
public static class Foo { public static class Foo {
private String name; private final String name;
public String getName() { public Foo(String name) {
return this.name; this.name = name;
} }
}
public static class Bar {
private final String name;
public void setName(String name) { public Bar(String name) {
this.name = name; this.name = name;
} }
} }
@ConfigurationProperties @ConfigurationProperties
...@@ -101,6 +128,8 @@ public class ConverterBindingTests { ...@@ -101,6 +128,8 @@ public class ConverterBindingTests {
private Foo foo; private Foo foo;
private Bar bar;
public Foo getFoo() { public Foo getFoo() {
return this.foo; return this.foo;
} }
...@@ -109,6 +138,13 @@ public class ConverterBindingTests { ...@@ -109,6 +138,13 @@ public class ConverterBindingTests {
this.foo = foo; this.foo = foo;
} }
public Bar getBar() {
return this.bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment