16
16
17
17
package org .springframework .boot .context .properties ;
18
18
19
+ import org .springframework .aop .scope .ScopedProxyUtils ;
19
20
import org .springframework .beans .factory .BeanFactory ;
20
21
import org .springframework .beans .factory .ListableBeanFactory ;
21
- import org .springframework .beans .factory .config .BeanDefinition ;
22
+ import org .springframework .beans .factory .annotation .AnnotatedGenericBeanDefinition ;
23
+ import org .springframework .beans .factory .config .BeanDefinitionHolder ;
24
+ import org .springframework .beans .factory .support .BeanDefinitionReaderUtils ;
22
25
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
23
- import org .springframework .beans .factory .support .RootBeanDefinition ;
24
26
import org .springframework .boot .context .properties .bind .BindMethod ;
27
+ import org .springframework .context .annotation .AnnotationScopeMetadataResolver ;
28
+ import org .springframework .context .annotation .ScopeMetadata ;
29
+ import org .springframework .context .annotation .ScopeMetadataResolver ;
30
+ import org .springframework .context .annotation .ScopedProxyMode ;
25
31
import org .springframework .core .annotation .MergedAnnotation ;
26
32
import org .springframework .core .annotation .MergedAnnotations ;
27
33
import org .springframework .core .annotation .MergedAnnotations .SearchStrategy ;
@@ -42,6 +48,8 @@ final class ConfigurationPropertiesBeanRegistrar {
42
48
43
49
private final BeanFactory beanFactory ;
44
50
51
+ private static final ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver ();
52
+
45
53
ConfigurationPropertiesBeanRegistrar (BeanDefinitionRegistry registry ) {
46
54
this .registry = registry ;
47
55
this .beanFactory = (BeanFactory ) this .registry ;
@@ -75,17 +83,25 @@ private void registerBeanDefinition(String beanName, Class<?> type,
75
83
MergedAnnotation <ConfigurationProperties > annotation ) {
76
84
Assert .state (annotation .isPresent (), () -> "No " + ConfigurationProperties .class .getSimpleName ()
77
85
+ " annotation found on '" + type .getName () + "'." );
78
- this . registry . registerBeanDefinition (beanName , createBeanDefinition (beanName , type ));
86
+ BeanDefinitionReaderUtils . registerBeanDefinition (createBeanDefinition (beanName , type ), this . registry );
79
87
}
80
88
81
- private BeanDefinition createBeanDefinition (String beanName , Class <?> type ) {
89
+ private BeanDefinitionHolder createBeanDefinition (String beanName , Class <?> type ) {
82
90
BindMethod bindMethod = ConfigurationPropertiesBean .deduceBindMethod (type );
83
- RootBeanDefinition definition = new RootBeanDefinition (type );
91
+ AnnotatedGenericBeanDefinition definition = new AnnotatedGenericBeanDefinition (type );
84
92
BindMethodAttribute .set (definition , bindMethod );
85
93
if (bindMethod == BindMethod .VALUE_OBJECT ) {
86
94
definition .setInstanceSupplier (() -> ConstructorBound .from (this .beanFactory , beanName , type ));
87
95
}
88
- return definition ;
96
+ ScopeMetadata metadata = scopeMetadataResolver .resolveScopeMetadata (definition );
97
+ definition .setScope (metadata .getScopeName ());
98
+ BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder (definition , beanName );
99
+ ScopedProxyMode scopedProxyMode = metadata .getScopedProxyMode ();
100
+ if (scopedProxyMode .equals (ScopedProxyMode .NO )) {
101
+ return definitionHolder ;
102
+ }
103
+ boolean proxyTargetClass = scopedProxyMode .equals (ScopedProxyMode .TARGET_CLASS );
104
+ return ScopedProxyUtils .createScopedProxy (definitionHolder , this .registry , proxyTargetClass );
89
105
}
90
106
91
107
}
0 commit comments