Skip to content

Support @DependsOn, @Description, @Fallback, @Lazy, @Primary, and @Role on @ConfigurationProperties beans #42289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.context.properties.bind.BindMethod;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.AnnotationScopeMetadataResolver;
import org.springframework.context.annotation.ScopeMetadata;
import org.springframework.context.annotation.ScopeMetadataResolver;
Expand All @@ -42,6 +42,7 @@
*
* @author Madhura Bhave
* @author Phillip Webb
* @author Yanming Zhou
*/
final class ConfigurationPropertiesBeanRegistrar {

Expand Down Expand Up @@ -88,7 +89,8 @@ private void registerBeanDefinition(String beanName, Class<?> type,
}

private BeanDefinitionHolder createBeanDefinition(String beanName, Class<?> type) {
GenericBeanDefinition definition = new AnnotatedGenericBeanDefinition(type);
AnnotatedGenericBeanDefinition definition = new AnnotatedGenericBeanDefinition(type);
AnnotationConfigUtils.processCommonDefinitionAnnotations(definition);
BindMethod bindMethod = ConfigurationPropertiesBean.deduceBindMethod(type);
BindMethodAttribute.set(definition, bindMethod);
if (bindMethod == BindMethod.VALUE_OBJECT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.context.properties.bind.BindMethod;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;

Expand All @@ -38,6 +39,7 @@
* @author Madhura Bhave
* @author Stephane Nicoll
* @author Phillip Webb
* @author Yanming Zhou
*/
class ConfigurationPropertiesBeanRegistrarTests {

Expand Down Expand Up @@ -122,6 +124,15 @@ void registerScopedBeanDefinitionWithProxyMode() {
assertThat(beanDefinition.getScope()).isEqualTo(BeanDefinition.SCOPE_PROTOTYPE);
}

@Test
void registerBeanDefinitionWithCommonDefinitionAnnotations() {
String beanName = "beancp-" + PrimaryConfigurationProperties.class.getName();
this.registrar.register(PrimaryConfigurationProperties.class);
BeanDefinition beanDefinition = this.registry.getBeanDefinition(beanName);
assertThat(beanDefinition).isNotNull();
assertThat(beanDefinition.isPrimary()).isEqualTo(true);
}

private Consumer<BeanDefinition> hasBindMethodAttribute(BindMethod bindMethod) {
return (definition) -> {
assertThat(definition.hasAttribute(BindMethod.class.getName())).isTrue();
Expand All @@ -146,6 +157,12 @@ static class ProxyScopedBeanConfigurationProperties {

}

@ConfigurationProperties(prefix = "beancp")
@Primary
static class PrimaryConfigurationProperties {

}

static class NoAnnotationConfigurationProperties {

}
Expand Down