Fix STS compatibility issues; other improvements
Revert changes to ParserContext, ReaderContext, and XmlReaderContext
These changes cause cross-version incompatibilities at tooling time
-- for instance, an STS version that ships with Spring 3.0.5
classloads the ParserContext defined in that version, whereas it
classloads NamespaceHandlers and BeanDefinitionParsers (by default)
from the user application classpath, which may be building against
3.1.0. If so, the changes introduced to these types in 3.1.0 are
incompatible with expectations in the 3.0.5 world and cause all
manner of problems. In this case, it was NoSuchMethodError due to
the newly-added XmlReaderContext.getProblemReporter() method; also
IncompatibleClassChangeError due to the introduction of the
ComponentRegistrar interface on ParserContext.
Each of these problems have been mitigated, though the solutions
are not ideal. The method mentioned has been removed, and instead
the problemReporter field is now accessed reflectively.
ParserContext now no longer implements ComponentRegistrar, and
rather a ComponentRegistrarAdapter class has been introduced that
passes method calls through to a ParserContext delegate.
Introduce AbstractSpecificationBeanDefinitionParser
AbstractSpecificationBeanDefinitionParser has been introduced in
order to improve the programming model for BeanDefinitionParsers
that have been refactored to the new FeatureSpecification model.
This new base class and it's template method implementation of
parse/doParse ensure that common concerns like (1) adapting a
ParserContext into a SpecificationContext, (2) setting source and
source name on the specification, and (3) actually executing the
specification are all managed by the base class. The subclass
implementation of doParse need only actually parse XML, populate
and return the FeatureSpecification object. This change removed
the many duplicate 'createSpecificationContext' methods that had
been lingering.
Minor improvement to BeanDefinitionReaderUtils API
Introduced new BeanDefinitionReaderUtils#registerWithGeneratedName
variant that accepts BeanDefinition as opposed to
AbstractBeanDefinition, as BeanDefinition is all that is actually
necessary to satisfy the needs of the method implementation. The
latter variant accepting AbstractBeanDefinition has been deprecated
but remains intact and delegates to the new variant in order to
maintain binary compatibility.
This commit is contained in:
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.beans.factory.xml.XmlReaderContext;
|
||||
import org.springframework.context.config.SpecificationContext;
|
||||
import org.springframework.context.config.AbstractSpecificationBeanDefinitionParser;
|
||||
import org.springframework.context.config.FeatureSpecification;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
@@ -38,11 +36,10 @@ import org.w3c.dom.NodeList;
|
||||
* @see ComponentScanSpec
|
||||
* @see ComponentScanExecutor
|
||||
*/
|
||||
public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
|
||||
public class ComponentScanBeanDefinitionParser extends AbstractSpecificationBeanDefinitionParser {
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
XmlReaderContext readerContext = parserContext.getReaderContext();
|
||||
ClassLoader classLoader = readerContext.getResourceLoader().getClassLoader();
|
||||
public FeatureSpecification doParse(Element element, ParserContext parserContext) {
|
||||
ClassLoader classLoader = parserContext.getReaderContext().getResourceLoader().getClassLoader();
|
||||
|
||||
ComponentScanSpec spec =
|
||||
ComponentScanSpec.forDelimitedPackages(element.getAttribute("base-package"))
|
||||
@@ -51,7 +48,9 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
|
||||
.resourcePattern(element.getAttribute("resource-pattern"))
|
||||
.beanNameGenerator(element.getAttribute("name-generator"), classLoader)
|
||||
.scopeMetadataResolver(element.getAttribute("scope-resolver"), classLoader)
|
||||
.scopedProxyMode(element.getAttribute("scoped-proxy"));
|
||||
.scopedProxyMode(element.getAttribute("scoped-proxy"))
|
||||
.beanDefinitionDefaults(parserContext.getDelegate().getBeanDefinitionDefaults())
|
||||
.autowireCandidatePatterns(parserContext.getDelegate().getAutowireCandidatePatterns());
|
||||
|
||||
// Parse exclude and include filter elements.
|
||||
NodeList nodeList = element.getChildNodes();
|
||||
@@ -70,26 +69,7 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
|
||||
}
|
||||
}
|
||||
|
||||
spec.beanDefinitionDefaults(parserContext.getDelegate().getBeanDefinitionDefaults())
|
||||
.autowireCandidatePatterns(parserContext.getDelegate().getAutowireCandidatePatterns())
|
||||
.source(readerContext.extractSource(element))
|
||||
.sourceName(element.getTagName())
|
||||
.execute(createSpecificationContext(parserContext));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Adapt the given ParserContext instance into an SpecificationContext.
|
||||
// TODO SPR-7420: create a common ParserContext-to-SpecificationContext adapter utility
|
||||
// or otherwise unify these two types
|
||||
private SpecificationContext createSpecificationContext(ParserContext parserContext) {
|
||||
SpecificationContext specificationContext = new SpecificationContext();
|
||||
specificationContext.setRegistry(parserContext.getRegistry());
|
||||
specificationContext.setRegistrar(parserContext);
|
||||
specificationContext.setResourceLoader(parserContext.getReaderContext().getResourceLoader());
|
||||
specificationContext.setEnvironment(parserContext.getDelegate().getEnvironment());
|
||||
specificationContext.setProblemReporter(parserContext.getReaderContext().getProblemReporter());
|
||||
return specificationContext;
|
||||
return spec;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -107,6 +107,7 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
this.sourceExtractor = sourceExtractor;
|
||||
this.problemReporter = problemReporter;
|
||||
this.metadataReaderFactory = metadataReaderFactory;
|
||||
// TODO SPR-7420: see about passing in the SpecificationContext created in ConfigurationClassPostProcessor
|
||||
this.specificationContext = new SpecificationContext();
|
||||
this.specificationContext.setRegistry(this.registry);
|
||||
this.specificationContext.setRegistrar(new SimpleComponentRegistrar(this.registry));
|
||||
@@ -171,7 +172,7 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
}
|
||||
|
||||
// no bean definition exists yet -> this must be an imported configuration class (@Import).
|
||||
GenericBeanDefinition configBeanDef = new GenericBeanDefinition();
|
||||
BeanDefinition configBeanDef = new GenericBeanDefinition();
|
||||
String className = configClass.getMetadata().getClassName();
|
||||
configBeanDef.setBeanClassName(className);
|
||||
if (checkConfigurationClassCandidate(configBeanDef, this.metadataReaderFactory)) {
|
||||
|
||||
@@ -354,8 +354,6 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
}
|
||||
}
|
||||
|
||||
// TODO SPR-7420: consider unifying the two through a superinterface.
|
||||
// TODO SPR-7420: create a common ParserContext-to-SpecificationContext adapter util
|
||||
private SpecificationContext createSpecificationContext(ConfigurableListableBeanFactory beanFactory) {
|
||||
final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
|
||||
SpecificationContext specificationContext = new SpecificationContext();
|
||||
@@ -363,8 +361,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
specificationContext.setResourceLoader(this.resourceLoader);
|
||||
specificationContext.setRegistry(registry);
|
||||
specificationContext.setRegistrar(new SimpleComponentRegistrar(registry));
|
||||
// TODO SPR-7420: how to get hold of the current problem reporter here?
|
||||
specificationContext.setProblemReporter(new FailFastProblemReporter());
|
||||
specificationContext.setProblemReporter(this.problemReporter);
|
||||
return specificationContext;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||
import org.springframework.beans.factory.parsing.ComponentDefinition;
|
||||
import org.springframework.beans.factory.parsing.ComponentRegistrar;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
|
||||
@@ -30,7 +29,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
*/
|
||||
public class SimpleComponentRegistrar implements ComponentRegistrar {
|
||||
class SimpleComponentRegistrar implements ComponentRegistrar {
|
||||
|
||||
private final BeanDefinitionRegistry registry;
|
||||
|
||||
@@ -39,7 +38,7 @@ public class SimpleComponentRegistrar implements ComponentRegistrar {
|
||||
}
|
||||
|
||||
public String registerWithGeneratedName(BeanDefinition beanDefinition) {
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName((AbstractBeanDefinition)beanDefinition, this.registry);
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, this.registry);
|
||||
}
|
||||
|
||||
public void registerBeanComponent(BeanComponentDefinition component) {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.config;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.parsing.ComponentRegistrarAdapter;
|
||||
import org.springframework.beans.factory.parsing.ProblemReporter;
|
||||
import org.springframework.beans.factory.parsing.ReaderContext;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* TODO SPR-7420: document
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
*/
|
||||
public abstract class AbstractSpecificationBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
public final BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
FeatureSpecification spec = doParse(element, parserContext);
|
||||
if (spec instanceof SourceAwareSpecification) {
|
||||
((SourceAwareSpecification)spec).source(parserContext.getReaderContext().extractSource(element));
|
||||
((SourceAwareSpecification)spec).sourceName(element.getTagName());
|
||||
}
|
||||
spec.execute(specificationContextFrom(parserContext));
|
||||
return null;
|
||||
}
|
||||
|
||||
abstract protected FeatureSpecification doParse(Element element, ParserContext parserContext);
|
||||
|
||||
/**
|
||||
* Adapt the given ParserContext into a SpecificationContext.
|
||||
*/
|
||||
private SpecificationContext specificationContextFrom(ParserContext parserContext) {
|
||||
SpecificationContext specContext = new SpecificationContext();
|
||||
specContext.setRegistry(parserContext.getRegistry());
|
||||
specContext.setRegistrar(new ComponentRegistrarAdapter(parserContext));
|
||||
specContext.setResourceLoader(parserContext.getReaderContext().getResourceLoader());
|
||||
specContext.setEnvironment(parserContext.getDelegate().getEnvironment());
|
||||
try {
|
||||
// access the reader context's problem reporter reflectively in order to
|
||||
// compensate for tooling (STS) constraints around introduction of changes
|
||||
// to parser context / reader context classes.
|
||||
Field field = ReaderContext.class.getDeclaredField("problemReporter");
|
||||
field.setAccessible(true);
|
||||
ProblemReporter problemReporter = (ProblemReporter)field.get(parserContext.getReaderContext());
|
||||
specContext.setProblemReporter(problemReporter);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException(
|
||||
"Could not access field 'ReaderContext#problemReporter' on object " +
|
||||
parserContext.getReaderContext(), ex);
|
||||
}
|
||||
return specContext;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user