16
16
package org .springframework .data .aot ;
17
17
18
18
import java .util .Collections ;
19
- import java .util .function .Function ;
20
19
import java .util .function .Supplier ;
21
20
22
21
import org .apache .commons .logging .Log ;
41
40
*
42
41
* @author Christoph Strobl
43
42
* @author John Blum
44
- * @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory
45
- * @see org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution
46
- * @see org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor
47
43
* @since 3.0
48
44
*/
49
- public class SpringDataBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
45
+ public class ManagedTypesBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
50
46
51
47
private static final Log logger = LogFactory .getLog (BeanFactoryInitializationAotProcessor .class );
52
48
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
-
62
49
@ Nullable
63
50
@ Override
64
51
public BeanFactoryInitializationAotContribution processAheadOfTime (ConfigurableListableBeanFactory beanFactory ) {
@@ -70,42 +57,61 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
70
57
private void processManagedTypes (ConfigurableListableBeanFactory beanFactory ) {
71
58
72
59
if (beanFactory instanceof BeanDefinitionRegistry registry ) {
60
+
73
61
for (String beanName : beanFactory .getBeanNamesForType (ManagedTypes .class )) {
62
+ postProcessManagedTypes (beanFactory , registry , beanName );
63
+ }
64
+ }
65
+ }
74
66
75
- BeanDefinition beanDefinition = beanFactory .getBeanDefinition (beanName );
67
+ private void postProcessManagedTypes (ConfigurableListableBeanFactory beanFactory , BeanDefinitionRegistry registry ,
68
+ String beanName ) {
76
69
77
- if ( hasConstructorArguments ( beanDefinition )) {
70
+ BeanDefinition beanDefinition = beanFactory . getBeanDefinition ( beanName );
78
71
79
- ValueHolder argumentValue = beanDefinition .getConstructorArgumentValues ()
80
- .getArgumentValue (0 , null , null , null );
72
+ if (hasConstructorArguments (beanDefinition )) {
81
73
82
- if ( argumentValue . getValue () instanceof Supplier supplier ) {
74
+ ValueHolder argumentValue = beanDefinition . getConstructorArgumentValues (). getArgumentValue ( 0 , null , null , null );
83
75
84
- if (logger .isDebugEnabled ()) {
85
- logger .info (String .format ("Replacing ManagedType bean definition %s." , beanName ));
86
- }
76
+ if (argumentValue .getValue ()instanceof Supplier supplier ) {
87
77
88
- Object value = constructorArgumentFunction .apply (supplier .get ());
78
+ if (logger .isDebugEnabled ()) {
79
+ logger .info (String .format ("Replacing ManagedType bean definition %s." , beanName ));
80
+ }
89
81
90
- BeanDefinition beanDefinitionReplacement = newManagedTypeBeanDefinition ( value );
82
+ Object value = potentiallyWrapToIterable ( supplier . get () );
91
83
92
- registry .removeBeanDefinition (beanName );
93
- registry .registerBeanDefinition (beanName , beanDefinitionReplacement );
94
- }
95
- }
84
+ BeanDefinition beanDefinitionReplacement = newManagedTypeBeanDefinition (beanDefinition .getBeanClassName (),
85
+ value );
86
+
87
+ registry .removeBeanDefinition (beanName );
88
+ registry .registerBeanDefinition (beanName , beanDefinitionReplacement );
96
89
}
97
90
}
98
91
}
99
92
93
+ private static Object potentiallyWrapToIterable (Object value ) {
94
+
95
+ if (ObjectUtils .isArray (value )) {
96
+ return CollectionUtils .arrayToList (value );
97
+ }
98
+
99
+ if (value instanceof Iterable <?>) {
100
+ return value ;
101
+ }
102
+
103
+ return Collections .singleton (value );
104
+ }
105
+
100
106
private boolean hasConstructorArguments (BeanDefinition beanDefinition ) {
101
107
return !beanDefinition .getConstructorArgumentValues ().isEmpty ();
102
108
}
103
109
104
- private BeanDefinition newManagedTypeBeanDefinition (Object constructorArgumentValue ) {
110
+ private BeanDefinition newManagedTypeBeanDefinition (String managedTypesClassName , Object constructorArgumentValue ) {
105
111
106
- return BeanDefinitionBuilder .rootBeanDefinition (ManagedTypes . class )
107
- .setFactoryMethod ("fromIterable" )
108
- .addConstructorArgValue (constructorArgumentValue )
109
- .getBeanDefinition ();
112
+ return BeanDefinitionBuilder .rootBeanDefinition (managedTypesClassName ) //
113
+ .setFactoryMethod ("fromIterable" ) //
114
+ .addConstructorArgValue (constructorArgumentValue ) //
115
+ .getBeanDefinition ();
110
116
}
111
117
}
0 commit comments