16
16
package org .springframework .data .aot ;
17
17
18
18
import java .util .Collections ;
19
+ import java .util .function .Function ;
19
20
import java .util .function .Supplier ;
20
21
21
22
import org .apache .commons .logging .Log ;
@@ -49,6 +50,15 @@ public class SpringDataBeanFactoryInitializationAotProcessor implements BeanFact
49
50
50
51
private static final Log logger = LogFactory .getLog (BeanFactoryInitializationAotProcessor .class );
51
52
53
+ private static final Function <Object , Object > arrayToListFunction = target ->
54
+ ObjectUtils .isArray (target ) ? CollectionUtils .arrayToList (target ) : target ;
55
+
56
+ private static final Function <Object , Object > asSingletonSetFunction = target ->
57
+ !(target instanceof Iterable <?>) ? Collections .singleton (target ) : target ;
58
+
59
+ private static final Function <Object , Object > constructorArgumentFunction =
60
+ arrayToListFunction .andThen (asSingletonSetFunction );
61
+
52
62
@ Nullable
53
63
@ Override
54
64
public BeanFactoryInitializationAotContribution processAheadOfTime (ConfigurableListableBeanFactory beanFactory ) {
@@ -64,33 +74,38 @@ private void processManagedTypes(ConfigurableListableBeanFactory beanFactory) {
64
74
65
75
BeanDefinition beanDefinition = beanFactory .getBeanDefinition (beanName );
66
76
67
- if (beanDefinition .getConstructorArgumentValues ().isEmpty ()) {
68
- return ;
69
- }
77
+ if (hasConstructorArguments (beanDefinition )) {
70
78
71
- ValueHolder argumentValue = beanDefinition .getConstructorArgumentValues ().getArgumentValue (0 , null , null , null );
79
+ ValueHolder argumentValue = beanDefinition .getConstructorArgumentValues ()
80
+ .getArgumentValue (0 , null , null , null );
72
81
73
- if (argumentValue .getValue ()instanceof Supplier supplier ) {
82
+ if (argumentValue .getValue () instanceof Supplier supplier ) {
74
83
75
- if (logger .isDebugEnabled ()) {
76
- logger .info (String .format ("Replacing ManagedType bean definition %s." , beanName ));
77
- }
84
+ if (logger .isDebugEnabled ()) {
85
+ logger .info (String .format ("Replacing ManagedType bean definition %s." , beanName ));
86
+ }
78
87
79
- Object value = supplier .get ();
80
- if (ObjectUtils .isArray (value )) {
81
- value = CollectionUtils .arrayToList (value );
82
- }
83
- if (!(value instanceof Iterable <?>)) {
84
- value = Collections .singleton (value );
85
- }
88
+ Object value = constructorArgumentFunction .apply (supplier .get ());
86
89
87
- BeanDefinition beanDefinitionReplacement = BeanDefinitionBuilder .rootBeanDefinition (ManagedTypes .class )
88
- .setFactoryMethod ("fromIterable" ).addConstructorArgValue (value ).getBeanDefinition ();
90
+ BeanDefinition beanDefinitionReplacement = newManagedTypeBeanDefinition (value );
89
91
90
- registry .removeBeanDefinition (beanName );
91
- registry .registerBeanDefinition (beanName , beanDefinitionReplacement );
92
+ registry .removeBeanDefinition (beanName );
93
+ registry .registerBeanDefinition (beanName , beanDefinitionReplacement );
94
+ }
92
95
}
93
96
}
94
97
}
95
98
}
99
+
100
+ private boolean hasConstructorArguments (BeanDefinition beanDefinition ) {
101
+ return !beanDefinition .getConstructorArgumentValues ().isEmpty ();
102
+ }
103
+
104
+ private BeanDefinition newManagedTypeBeanDefinition (Object constructorArgumentValue ) {
105
+
106
+ return BeanDefinitionBuilder .rootBeanDefinition (ManagedTypes .class )
107
+ .setFactoryMethod ("fromIterable" )
108
+ .addConstructorArgValue (constructorArgumentValue )
109
+ .getBeanDefinition ();
110
+ }
96
111
}
0 commit comments