ImportRegistry as a package-visible top-level interface which can easily be shared within the package
Issue: SPR-12128
This commit is contained in:
parent
125ae99035
commit
6563405be5
|
|
@ -44,7 +44,6 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
import org.springframework.context.annotation.ConfigurationClassParser.ImportRegistry;
|
|
||||||
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
|
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
|
||||||
import org.springframework.core.annotation.AnnotationAttributes;
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
|
@ -96,7 +95,7 @@ class ConfigurationClassBeanDefinitionReader {
|
||||||
* Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used
|
* Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used
|
||||||
* to populate the given {@link BeanDefinitionRegistry}.
|
* to populate the given {@link BeanDefinitionRegistry}.
|
||||||
*/
|
*/
|
||||||
public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor,
|
ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor,
|
||||||
ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory,
|
ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory,
|
||||||
ResourceLoader resourceLoader, Environment environment, BeanNameGenerator importBeanNameGenerator,
|
ResourceLoader resourceLoader, Environment environment, BeanNameGenerator importBeanNameGenerator,
|
||||||
ImportRegistry importRegistry) {
|
ImportRegistry importRegistry) {
|
||||||
|
|
@ -132,8 +131,11 @@ class ConfigurationClassBeanDefinitionReader {
|
||||||
TrackedConditionEvaluator trackedConditionEvaluator) {
|
TrackedConditionEvaluator trackedConditionEvaluator) {
|
||||||
|
|
||||||
if (trackedConditionEvaluator.shouldSkip(configClass)) {
|
if (trackedConditionEvaluator.shouldSkip(configClass)) {
|
||||||
removeBeanDefinition(configClass);
|
String beanName = configClass.getBeanName();
|
||||||
importRegistry.removeImportingClassFor(configClass.getMetadata().getClassName());
|
if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
|
||||||
|
this.registry.removeBeanDefinition(beanName);
|
||||||
|
}
|
||||||
|
this.importRegistry.removeImportingClassFor(configClass.getMetadata().getClassName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,13 +149,6 @@ class ConfigurationClassBeanDefinitionReader {
|
||||||
loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
|
loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeBeanDefinition(ConfigurationClass configClass) {
|
|
||||||
String beanName = configClass.getBeanName();
|
|
||||||
if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
|
|
||||||
this.registry.removeBeanDefinition(beanName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the {@link Configuration} class itself as a bean definition.
|
* Register the {@link Configuration} class itself as a bean definition.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -588,14 +588,6 @@ class ConfigurationClassParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface ImportRegistry {
|
|
||||||
|
|
||||||
AnnotationMetadata getImportingClassFor(String importedClass);
|
|
||||||
|
|
||||||
void removeImportingClassFor(String importedClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
private static class ImportStack extends Stack<ConfigurationClass> implements ImportRegistry {
|
private static class ImportStack extends Stack<ConfigurationClass> implements ImportRegistry {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,6 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.context.EnvironmentAware;
|
import org.springframework.context.EnvironmentAware;
|
||||||
import org.springframework.context.ResourceLoaderAware;
|
import org.springframework.context.ResourceLoaderAware;
|
||||||
import org.springframework.context.annotation.ConfigurationClassEnhancer.EnhancedConfiguration;
|
import org.springframework.context.annotation.ConfigurationClassEnhancer.EnhancedConfiguration;
|
||||||
import org.springframework.context.annotation.ConfigurationClassParser.ImportRegistry;
|
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.PriorityOrdered;
|
import org.springframework.core.PriorityOrdered;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2014 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.context.annotation;
|
||||||
|
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @author Phil Webb
|
||||||
|
*/
|
||||||
|
interface ImportRegistry {
|
||||||
|
|
||||||
|
AnnotationMetadata getImportingClassFor(String importedClass);
|
||||||
|
|
||||||
|
void removeImportingClassFor(String importedClass);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -281,8 +281,7 @@ public class ImportAwareTests {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
return context.getBeanFactory().getBeanNamesForType(MetadataHolder.class,
|
return (context.getBeanFactory().getBeanNamesForType(MetadataHolder.class, true, false).length == 0);
|
||||||
true, false).length == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue