|
17 | 17 | package org.springframework.test.context.bean.override;
|
18 | 18 |
|
19 | 19 | import java.util.Set;
|
20 |
| -import java.util.function.Consumer; |
21 | 20 |
|
22 |
| -import org.springframework.beans.factory.config.BeanDefinition; |
23 |
| -import org.springframework.beans.factory.config.ConstructorArgumentValues; |
24 |
| -import org.springframework.beans.factory.config.RuntimeBeanReference; |
25 |
| -import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
26 |
| -import org.springframework.beans.factory.support.RootBeanDefinition; |
| 21 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
27 | 22 | import org.springframework.context.ConfigurableApplicationContext;
|
28 | 23 | import org.springframework.test.context.ContextCustomizer;
|
29 | 24 | import org.springframework.test.context.MergedContextConfiguration;
|
@@ -57,43 +52,18 @@ class BeanOverrideContextCustomizer implements ContextCustomizer {
|
57 | 52 |
|
58 | 53 | @Override
|
59 | 54 | public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
|
60 |
| - if (!(context instanceof BeanDefinitionRegistry registry)) { |
61 |
| - throw new IllegalStateException("Cannot process bean overrides with an ApplicationContext " + |
62 |
| - "that doesn't implement BeanDefinitionRegistry: " + context.getClass()); |
63 |
| - } |
64 |
| - registerInfrastructure(registry); |
| 55 | + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); |
| 56 | + BeanOverrideRegistrar beanOverrideRegistrar = new BeanOverrideRegistrar(); |
| 57 | + beanOverrideRegistrar.setBeanFactory(beanFactory); |
| 58 | + beanFactory.registerSingleton(REGISTRAR_BEAN_NAME, beanOverrideRegistrar); |
| 59 | + beanFactory.registerSingleton(EARLY_INFRASTRUCTURE_BEAN_NAME, new WrapEarlyBeanPostProcessor(beanOverrideRegistrar)); |
| 60 | + beanFactory.registerSingleton(INFRASTRUCTURE_BEAN_NAME, new BeanOverrideBeanFactoryPostProcessor(this.metadata, beanOverrideRegistrar)); |
65 | 61 | }
|
66 | 62 |
|
67 | 63 | Set<OverrideMetadata> getMetadata() {
|
68 | 64 | return this.metadata;
|
69 | 65 | }
|
70 | 66 |
|
71 |
| - private void registerInfrastructure(BeanDefinitionRegistry registry) { |
72 |
| - addInfrastructureBeanDefinition(registry, BeanOverrideRegistrar.class, REGISTRAR_BEAN_NAME, |
73 |
| - constructorArgs -> {}); |
74 |
| - |
75 |
| - RuntimeBeanReference registrarReference = new RuntimeBeanReference(REGISTRAR_BEAN_NAME); |
76 |
| - addInfrastructureBeanDefinition(registry, WrapEarlyBeanPostProcessor.class, EARLY_INFRASTRUCTURE_BEAN_NAME, |
77 |
| - constructorArgs -> constructorArgs.addIndexedArgumentValue(0, registrarReference)); |
78 |
| - addInfrastructureBeanDefinition(registry, BeanOverrideBeanFactoryPostProcessor.class, INFRASTRUCTURE_BEAN_NAME, |
79 |
| - constructorArgs -> { |
80 |
| - constructorArgs.addIndexedArgumentValue(0, this.metadata); |
81 |
| - constructorArgs.addIndexedArgumentValue(1, registrarReference); |
82 |
| - }); |
83 |
| - } |
84 |
| - |
85 |
| - private void addInfrastructureBeanDefinition(BeanDefinitionRegistry registry, |
86 |
| - Class<?> clazz, String beanName, Consumer<ConstructorArgumentValues> constructorArgumentsConsumer) { |
87 |
| - |
88 |
| - if (!registry.containsBeanDefinition(beanName)) { |
89 |
| - RootBeanDefinition definition = new RootBeanDefinition(clazz); |
90 |
| - definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
91 |
| - ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues(); |
92 |
| - constructorArgumentsConsumer.accept(constructorArguments); |
93 |
| - registry.registerBeanDefinition(beanName, definition); |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 | 67 | @Override
|
98 | 68 | public boolean equals(Object other) {
|
99 | 69 | if (other == this) {
|
|
0 commit comments