Relax ImportsContextCustomizer to only require a class name

Closes gh-32421
pull/32205/head
Stephane Nicoll 2 years ago
parent 0916736629
commit 190d4cbc2f

@ -60,14 +60,14 @@ import org.springframework.util.ReflectionUtils;
*/ */
class ImportsContextCustomizer implements ContextCustomizer { class ImportsContextCustomizer implements ContextCustomizer {
static final String TEST_CLASS_ATTRIBUTE = "testClass"; private static final String TEST_CLASS_NAME_ATTRIBUTE = "testClassName";
private final Class<?> testClass; private final String testClassName;
private final ContextCustomizerKey key; private final ContextCustomizerKey key;
ImportsContextCustomizer(Class<?> testClass) { ImportsContextCustomizer(Class<?> testClass) {
this.testClass = testClass; this.testClassName = testClass.getName();
this.key = new ContextCustomizerKey(testClass); this.key = new ContextCustomizerKey(testClass);
} }
@ -84,13 +84,13 @@ class ImportsContextCustomizer implements ContextCustomizer {
BeanDefinition definition = registerBean(registry, reader, ImportsCleanupPostProcessor.BEAN_NAME, BeanDefinition definition = registerBean(registry, reader, ImportsCleanupPostProcessor.BEAN_NAME,
ImportsCleanupPostProcessor.class); ImportsCleanupPostProcessor.class);
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, this.testClass); definition.getConstructorArgumentValues().addIndexedArgumentValue(0, this.testClassName);
} }
private void registerImportsConfiguration(BeanDefinitionRegistry registry, AnnotatedBeanDefinitionReader reader) { private void registerImportsConfiguration(BeanDefinitionRegistry registry, AnnotatedBeanDefinitionReader reader) {
BeanDefinition definition = registerBean(registry, reader, ImportsConfiguration.BEAN_NAME, BeanDefinition definition = registerBean(registry, reader, ImportsConfiguration.BEAN_NAME,
ImportsConfiguration.class); ImportsConfiguration.class);
definition.setAttribute(TEST_CLASS_ATTRIBUTE, this.testClass); definition.setAttribute(TEST_CLASS_NAME_ATTRIBUTE, this.testClassName);
} }
private BeanDefinitionRegistry getBeanDefinitionRegistry(ApplicationContext context) { private BeanDefinitionRegistry getBeanDefinitionRegistry(ApplicationContext context) {
@ -162,8 +162,8 @@ class ImportsContextCustomizer implements ContextCustomizer {
@Override @Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) { public String[] selectImports(AnnotationMetadata importingClassMetadata) {
BeanDefinition definition = this.beanFactory.getBeanDefinition(ImportsConfiguration.BEAN_NAME); BeanDefinition definition = this.beanFactory.getBeanDefinition(ImportsConfiguration.BEAN_NAME);
Object testClass = (definition != null) ? definition.getAttribute(TEST_CLASS_ATTRIBUTE) : null; Object testClassName = definition.getAttribute(TEST_CLASS_NAME_ATTRIBUTE);
return (testClass != null) ? new String[] { ((Class<?>) testClass).getName() } : NO_IMPORTS; return (testClassName != null) ? new String[] { (String) testClassName } : NO_IMPORTS;
} }
} }
@ -177,10 +177,10 @@ class ImportsContextCustomizer implements ContextCustomizer {
static final String BEAN_NAME = ImportsCleanupPostProcessor.class.getName(); static final String BEAN_NAME = ImportsCleanupPostProcessor.class.getName();
private final Class<?> testClass; private final String testClassName;
ImportsCleanupPostProcessor(Class<?> testClass) { ImportsCleanupPostProcessor(String testClassName) {
this.testClass = testClass; this.testClassName = testClassName;
} }
@Override @Override
@ -193,7 +193,7 @@ class ImportsContextCustomizer implements ContextCustomizer {
String[] names = registry.getBeanDefinitionNames(); String[] names = registry.getBeanDefinitionNames();
for (String name : names) { for (String name : names) {
BeanDefinition definition = registry.getBeanDefinition(name); BeanDefinition definition = registry.getBeanDefinition(name);
if (this.testClass.getName().equals(definition.getBeanClassName())) { if (this.testClassName.equals(definition.getBeanClassName())) {
registry.removeBeanDefinition(name); registry.removeBeanDefinition(name);
} }
} }

Loading…
Cancel
Save