mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Make @ConfigurationProperties available in @PostConstruct
Also means that persistence annotation processing can take advantage of external properties bound in this way.
This commit is contained in:
parent
99a2338323
commit
3e9457ea8e
@ -16,7 +16,8 @@
|
||||
|
||||
package sample.data.mongo;
|
||||
|
||||
import java.io.IOException;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.ConnectException;
|
||||
|
||||
import org.junit.Rule;
|
||||
@ -24,8 +25,6 @@ import org.junit.Test;
|
||||
import org.springframework.boot.test.OutputCapture;
|
||||
import org.springframework.core.NestedCheckedException;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link SampleMongoApplication}.
|
||||
*
|
||||
|
@ -36,6 +36,7 @@ import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.PriorityOrdered;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
@ -66,7 +67,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
*/
|
||||
public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor,
|
||||
BeanFactoryAware, ResourceLoaderAware, EnvironmentAware, ApplicationContextAware,
|
||||
InitializingBean, DisposableBean, Ordered {
|
||||
InitializingBean, DisposableBean, PriorityOrdered {
|
||||
|
||||
public static final String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";
|
||||
|
||||
@ -93,7 +94,7 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE;
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE + 1;
|
||||
|
||||
/**
|
||||
* @param order the order to set
|
||||
@ -271,21 +272,21 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
ConfigurationProperties annotation = AnnotationUtils.findAnnotation(
|
||||
bean.getClass(), ConfigurationProperties.class);
|
||||
if (annotation != null || bean instanceof ConfigurationPropertiesHolder) {
|
||||
postProcessBeforeInitialization(bean, beanName, annotation);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
ConfigurationProperties annotation = AnnotationUtils.findAnnotation(
|
||||
bean.getClass(), ConfigurationProperties.class);
|
||||
if (annotation != null || bean instanceof ConfigurationPropertiesHolder) {
|
||||
postProcessAfterInitialization(bean, beanName, annotation);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private void postProcessAfterInitialization(Object bean, String beanName,
|
||||
private void postProcessBeforeInitialization(Object bean, String beanName,
|
||||
ConfigurationProperties annotation) {
|
||||
Object target = (bean instanceof ConfigurationPropertiesHolder ? ((ConfigurationPropertiesHolder) bean)
|
||||
.getTarget() : bean);
|
||||
|
@ -16,11 +16,13 @@
|
||||
|
||||
package org.springframework.boot.context.properties;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
@ -28,8 +30,8 @@ import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
@ -40,7 +42,7 @@ import static org.junit.Assert.assertTrue;
|
||||
*/
|
||||
public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
@ -51,7 +53,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||
|
||||
@Test
|
||||
public void testValidationWithoutJSR303() {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfigurationWithoutJSR303.class);
|
||||
try {
|
||||
this.context.refresh();
|
||||
@ -64,7 +66,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||
|
||||
@Test
|
||||
public void testValidationWithJSR303() {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfigurationWithJSR303.class);
|
||||
try {
|
||||
this.context.refresh();
|
||||
@ -80,12 +82,22 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||
MockEnvironment env = new MockEnvironment();
|
||||
env.setProperty("test.foo", "123456");
|
||||
env.setProperty("test.bar", "654321");
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.setEnvironment(env);
|
||||
this.context.register(TestConfigurationWithJSR303.class);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitializersSeeBoundProperties() {
|
||||
MockEnvironment env = new MockEnvironment();
|
||||
env.setProperty("bar", "foo");
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.setEnvironment(env);
|
||||
this.context.register(TestConfigurationWithInitializer.class);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class TestConfigurationWithoutJSR303 {
|
||||
@ -133,6 +145,28 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties
|
||||
public static class TestConfigurationWithInitializer {
|
||||
|
||||
private String bar;
|
||||
|
||||
public void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public String getBar() {
|
||||
return this.bar;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
assertNotNull(this.bar);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties(name = "test")
|
||||
public static class PropertyWithJSR303 extends PropertyWithoutJSR303 {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user