Top Description Fields Constructors Methods
org.springframework.beans.factory.xml

public Class XmlBeanDefinitionReader

extends AbstractBeanDefinitionReader
Class Inheritance
Imports
java.io.IOException, .InputStream, java.util.HashSet, .Map, .Set, javax.xml.parsers.ParserConfigurationException, org.w3c.dom.Document, org.xml.sax.EntityResolver, .ErrorHandler, .InputSource, .SAXException, .SAXParseException, org.springframework.beans.BeanUtils, org.springframework.beans.factory.BeanDefinitionStoreException, org.springframework.beans.factory.parsing.EmptyReaderEventListener, .FailFastProblemReporter, .NullSourceExtractor, .ProblemReporter, .ReaderEventListener, .SourceExtractor, org.springframework.beans.factory.support.AbstractBeanDefinitionReader, .BeanDefinitionRegistry, org.springframework.core.NamedThreadLocal, org.springframework.core.io.DescriptiveResource, .Resource, .ResourceLoader, org.springframework.core.io.support.EncodedResource, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.util.xml.SimpleSaxErrorHandler, .XmlValidationModeDetector

Bean definition reader for XML bean definitions. Delegates the actual XML document reading to an implementation of the BeanDefinitionDocumentReader interface.

Typically applied to a org.springframework.beans.factory.support.DefaultListableBeanFactory or a org.springframework.context.support.GenericApplicationContext.

This class loads a DOM document and applies the BeanDefinitionDocumentReader to it. The document reader will register each bean definition with the given bean factory, talking to the latter's implementation of the org.springframework.beans.factory.support.BeanDefinitionRegistry interface.

Authors
Juergen Hoeller, Rob Harrop, Chris Beams, Sam Brannen
Since
26.11.2003
See Also
setDocumentReaderClass, BeanDefinitionDocumentReader, DefaultBeanDefinitionDocumentReader, BeanDefinitionRegistry, org.springframework.beans.factory.support.DefaultListableBeanFactory, org.springframework.context.support.GenericApplicationContext

Field Summary

Modifier and TypeField and Description
private static final Map<String, Integer>
constants

Map of constant names to constant values for the validation constants defined in this class.

private DocumentLoader
private Class<? extends BeanDefinitionDocumentReader>
private EntityResolver
private ErrorHandler
private ReaderEventListener
private boolean
private NamespaceHandlerResolver
private ProblemReporter
private final ThreadLocal<Set<EncodedResource>>
private SourceExtractor
public static final int
VALIDATION_AUTO

Indicates that the validation mode should be detected automatically.

public static final int
VALIDATION_DTD

Indicates that DTD validation should be used.

public static final int
VALIDATION_NONE

Indicates that the validation should be disabled.

public static final int
VALIDATION_XSD

Indicates that XSD validation should be used.

private int
private final XmlValidationModeDetector
Inherited from org.springframework.beans.factory.support.AbstractBeanDefinitionReader:
logger

Constructor Summary

AccessConstructor and Description
public
XmlBeanDefinitionReader(BeanDefinitionRegistry
the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry
registry
)

Create new XmlBeanDefinitionReader for the given bean factory.

Method Summary

Modifier and TypeMethod and Description
protected BeanDefinitionDocumentReader
createBeanDefinitionDocumentReader()

Create the BeanDefinitionDocumentReader to use for actually reading bean definitions from an XML document.

protected NamespaceHandlerResolver
createDefaultNamespaceHandlerResolver()

Create the default implementation of NamespaceHandlerResolver used if none is specified.

public XmlReaderContext
createReaderContext(Resource resource)

Create the XmlReaderContext to pass over to the document reader.

protected int
detectValidationMode(Resource resource)

Detect which kind of validation to perform on the XML file identified by the supplied Resource.

protected int

Returns:

the number of bean definitions found
doLoadBeanDefinitions
(InputSource
the SAX InputSource to read from
inputSource
,
Resource
the resource descriptor for the XML file
resource
)

Actually load bean definitions from the specified XML file.

protected Document

Returns:

the DOM Document
doLoadDocument
(InputSource
the SAX InputSource to read from
inputSource
,
Resource
the resource descriptor for the XML file
resource
)

Actually load the specified document using the configured DocumentLoader.

protected EntityResolver
getEntityResolver()

Return the EntityResolver to use, building a default resolver if none specified.

public NamespaceHandlerResolver
getNamespaceHandlerResolver()

Lazily create a default NamespaceHandlerResolver, if not set before.

public int
getValidationMode()

Return the validation mode to use.

protected int
getValidationModeForResource(Resource resource)

Determine the validation mode for the specified Resource.

public boolean
isNamespaceAware()

Return whether the XML parser should be XML namespace aware.

public int

Returns:

the number of bean definitions found
loadBeanDefinitions
(Resource
the resource descriptor for the XML file
resource
)

Implements org.springframework.beans.factory.support.BeanDefinitionReader.loadBeanDefinitions.

Load bean definitions from the specified XML file.
public int

Returns:

the number of bean definitions found
loadBeanDefinitions
(EncodedResource
the resource descriptor for the XML file, allowing to specify an encoding to use for parsing the file
encodedResource
)

Load bean definitions from the specified XML file.

public int

Returns:

the number of bean definitions found
loadBeanDefinitions
(InputSource
the SAX InputSource to read from
inputSource
)

Load bean definitions from the specified XML file.

public int

Returns:

the number of bean definitions found
loadBeanDefinitions
(InputSource
the SAX InputSource to read from
inputSource
,
String
a description of the resource (can be null or empty)
resourceDescription
)

Load bean definitions from the specified XML file.

public int

Returns:

the number of bean definitions found
registerBeanDefinitions
(Document
the DOM document
doc
,
Resource
the resource descriptor (for context information)
resource
)

Register the bean definitions contained in the given DOM document.

public void
setDocumentLoader(DocumentLoader documentLoader)

Specify the DocumentLoader to use.

public void
setDocumentReaderClass(Class<? extends BeanDefinitionDocumentReader>
the desired BeanDefinitionDocumentReader implementation class
documentReaderClass
)

Specify the BeanDefinitionDocumentReader implementation to use, responsible for the actual reading of the XML bean definition document.

public void
setEntityResolver(EntityResolver entityResolver)

Set a SAX entity resolver to be used for parsing.

public void
setErrorHandler(ErrorHandler errorHandler)

Set an implementation of the org.xml.sax.ErrorHandler interface for custom handling of XML parsing errors and warnings.

public void
setEventListener(ReaderEventListener eventListener)

Specify which ReaderEventListener to use.

public void
setNamespaceAware(boolean namespaceAware)

Set whether the XML parser should be XML namespace aware.

public void
public void
public void
setSourceExtractor(SourceExtractor sourceExtractor)

Specify the SourceExtractor to use.

public void
setValidating(boolean validating)

Set whether to use XML validation.

public void
setValidationMode(int validationMode)

Set the validation mode to use.

public void
setValidationModeName(String validationModeName)

Set the validation mode to use by name.

Inherited from org.springframework.beans.factory.support.AbstractBeanDefinitionReader:
getBeanClassLoadergetBeanNameGeneratorgetEnvironmentgetRegistrygetResourceLoaderloadBeanDefinitionsloadBeanDefinitionsloadBeanDefinitionsloadBeanDefinitionssetBeanClassLoadersetBeanNameGeneratorsetEnvironmentsetResourceLoader

Field Detail

constantsback to summary
private static final Map<String, Integer> constants

Map of constant names to constant values for the validation constants defined in this class.

documentLoaderback to summary
private DocumentLoader documentLoader
documentReaderClassback to summary
private Class<? extends BeanDefinitionDocumentReader> documentReaderClass
entityResolverback to summary
private EntityResolver entityResolver
Annotations
@Nullable
errorHandlerback to summary
private ErrorHandler errorHandler
eventListenerback to summary
private ReaderEventListener eventListener
namespaceAwareback to summary
private boolean namespaceAware
namespaceHandlerResolverback to summary
private NamespaceHandlerResolver namespaceHandlerResolver
Annotations
@Nullable
problemReporterback to summary
private ProblemReporter problemReporter
resourcesCurrentlyBeingLoadedback to summary
private final ThreadLocal<Set<EncodedResource>> resourcesCurrentlyBeingLoaded
sourceExtractorback to summary
private SourceExtractor sourceExtractor
VALIDATION_AUTOback to summary
public static final int VALIDATION_AUTO

Indicates that the validation mode should be detected automatically.

VALIDATION_DTDback to summary
public static final int VALIDATION_DTD

Indicates that DTD validation should be used.

VALIDATION_NONEback to summary
public static final int VALIDATION_NONE

Indicates that the validation should be disabled.

VALIDATION_XSDback to summary
public static final int VALIDATION_XSD

Indicates that XSD validation should be used.

validationModeback to summary
private int validationMode
validationModeDetectorback to summary
private final XmlValidationModeDetector validationModeDetector

Constructor Detail

XmlBeanDefinitionReaderback to summary
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry)

Create new XmlBeanDefinitionReader for the given bean factory.

Parameters
registry:BeanDefinitionRegistry

the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry

Method Detail

createBeanDefinitionDocumentReaderback to summary
protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader()

Create the BeanDefinitionDocumentReader to use for actually reading bean definitions from an XML document.

The default implementation instantiates the specified "documentReaderClass".

See Also
setDocumentReaderClass
createDefaultNamespaceHandlerResolverback to summary
protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver()

Create the default implementation of NamespaceHandlerResolver used if none is specified.

The default implementation returns an instance of DefaultNamespaceHandlerResolver.

See Also
DefaultNamespaceHandlerResolver#DefaultNamespaceHandlerResolver(ClassLoader)
createReaderContextback to summary
public XmlReaderContext createReaderContext(Resource resource)

Create the XmlReaderContext to pass over to the document reader.

detectValidationModeback to summary
protected int detectValidationMode(Resource resource)

Detect which kind of validation to perform on the XML file identified by the supplied Resource. If the file has a DOCTYPE definition then DTD validation is used otherwise XSD validation is assumed.

Override this method if you would like to customize resolution of the VALIDATION_AUTO mode.

doLoadBeanDefinitionsback to summary
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException

Actually load bean definitions from the specified XML file.

Parameters
inputSource:InputSource

the SAX InputSource to read from

resource:Resource

the resource descriptor for the XML file

Returns:int

the number of bean definitions found

Exceptions
BeanDefinitionStoreException:
in case of loading or parsing errors
See Also
doLoadDocument, registerBeanDefinitions
doLoadDocumentback to summary
protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception

Actually load the specified document using the configured DocumentLoader.

Parameters
inputSource:InputSource

the SAX InputSource to read from

resource:Resource

the resource descriptor for the XML file

Returns:Document

the DOM Document

Exceptions
Exception:
when thrown from the DocumentLoader
See Also
setDocumentLoader, DocumentLoader#loadDocument
getEntityResolverback to summary
protected EntityResolver getEntityResolver()

Return the EntityResolver to use, building a default resolver if none specified.

getNamespaceHandlerResolverback to summary
public NamespaceHandlerResolver getNamespaceHandlerResolver()

Lazily create a default NamespaceHandlerResolver, if not set before.

See Also
createDefaultNamespaceHandlerResolver()
getValidationModeback to summary
public int getValidationMode()

Return the validation mode to use.

getValidationModeForResourceback to summary
protected int getValidationModeForResource(Resource resource)

Determine the validation mode for the specified Resource. If no explicit validation mode has been configured, then the validation mode gets detected from the given resource.

Override this method if you would like full control over the validation mode, even when something other than VALIDATION_AUTO was set.

See Also
detectValidationMode
isNamespaceAwareback to summary
public boolean isNamespaceAware()

Return whether the XML parser should be XML namespace aware.

loadBeanDefinitionsback to summary
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException

Implements org.springframework.beans.factory.support.BeanDefinitionReader.loadBeanDefinitions.

Load bean definitions from the specified XML file.

Parameters
resource:Resource

the resource descriptor for the XML file

Returns:int

the number of bean definitions found

Annotations
@Override
Exceptions
BeanDefinitionStoreException:
in case of loading or parsing errors
loadBeanDefinitionsback to summary
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException

Load bean definitions from the specified XML file.

Parameters
encodedResource:EncodedResource

the resource descriptor for the XML file, allowing to specify an encoding to use for parsing the file

Returns:int

the number of bean definitions found

Exceptions
BeanDefinitionStoreException:
in case of loading or parsing errors
loadBeanDefinitionsback to summary
public int loadBeanDefinitions(InputSource inputSource) throws BeanDefinitionStoreException

Load bean definitions from the specified XML file.

Parameters
inputSource:InputSource

the SAX InputSource to read from

Returns:int

the number of bean definitions found

Exceptions
BeanDefinitionStoreException:
in case of loading or parsing errors
loadBeanDefinitionsback to summary
public int loadBeanDefinitions(InputSource inputSource, String resourceDescription) throws BeanDefinitionStoreException

Load bean definitions from the specified XML file.

Parameters
inputSource:InputSource

the SAX InputSource to read from

resourceDescription:String

a description of the resource (can be null or empty)

Returns:int

the number of bean definitions found

Exceptions
BeanDefinitionStoreException:
in case of loading or parsing errors
registerBeanDefinitionsback to summary
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException

Register the bean definitions contained in the given DOM document. Called by loadBeanDefinitions.

Creates a new instance of the parser class and invokes registerBeanDefinitions on it.

Parameters
doc:Document

the DOM document

resource:Resource

the resource descriptor (for context information)

Returns:int

the number of bean definitions found

Exceptions
BeanDefinitionStoreException:
in case of parsing errors
See Also
loadBeanDefinitions, setDocumentReaderClass, BeanDefinitionDocumentReader#registerBeanDefinitions
setDocumentLoaderback to summary
public void setDocumentLoader(DocumentLoader documentLoader)

Specify the DocumentLoader to use.

The default implementation is DefaultDocumentLoader which loads Document instances using JAXP.

setDocumentReaderClassback to summary
public void setDocumentReaderClass(Class<? extends BeanDefinitionDocumentReader> documentReaderClass)

Specify the BeanDefinitionDocumentReader implementation to use, responsible for the actual reading of the XML bean definition document.

The default is DefaultBeanDefinitionDocumentReader.

Parameters
documentReaderClass:Class<? extends BeanDefinitionDocumentReader>

the desired BeanDefinitionDocumentReader implementation class

setEntityResolverback to summary
public void setEntityResolver(EntityResolver entityResolver)

Set a SAX entity resolver to be used for parsing.

By default, ResourceEntityResolver will be used. Can be overridden for custom entity resolution, for example relative to some specific base path.

setErrorHandlerback to summary
public void setErrorHandler(ErrorHandler errorHandler)

Set an implementation of the org.xml.sax.ErrorHandler interface for custom handling of XML parsing errors and warnings.

If not set, a default SimpleSaxErrorHandler is used that simply logs warnings using the logger instance of the view class, and rethrows errors to discontinue the XML transformation.

See Also
SimpleSaxErrorHandler
setEventListenerback to summary
public void setEventListener(ReaderEventListener eventListener)

Specify which ReaderEventListener to use.

The default implementation is EmptyReaderEventListener which discards every event notification. External tools can provide an alternative implementation to monitor the components being registered in the BeanFactory.

setNamespaceAwareback to summary
public void setNamespaceAware(boolean namespaceAware)

Set whether the XML parser should be XML namespace aware. Default is "false".

This is typically not needed when schema validation is active. However, without validation, this has to be switched to "true" in order to properly process schema namespaces.

setNamespaceHandlerResolverback to summary
public void setNamespaceHandlerResolver(NamespaceHandlerResolver namespaceHandlerResolver)

Specify the NamespaceHandlerResolver to use.

If none is specified, a default instance will be created through createDefaultNamespaceHandlerResolver().

setProblemReporterback to summary
public void setProblemReporter(ProblemReporter problemReporter)

Specify which org.springframework.beans.factory.parsing.ProblemReporter to use.

The default implementation is org.springframework.beans.factory.parsing.FailFastProblemReporter which exhibits fail fast behaviour. External tools can provide an alternative implementation that collates errors and warnings for display in the tool UI.

setSourceExtractorback to summary
public void setSourceExtractor(SourceExtractor sourceExtractor)

Specify the SourceExtractor to use.

The default implementation is NullSourceExtractor which simply returns null as the source object. This means that - during normal runtime execution - no additional source metadata is attached to the bean configuration metadata.

setValidatingback to summary
public void setValidating(boolean validating)

Set whether to use XML validation. Default is true.

This method switches namespace awareness on if validation is turned off, in order to still process schema namespaces properly in such a scenario.

See Also
setValidationMode, setNamespaceAware
setValidationModeback to summary
public void setValidationMode(int validationMode)

Set the validation mode to use. Defaults to VALIDATION_AUTO.

Note that this only activates or deactivates validation itself. If you are switching validation off for schema files, you might need to activate schema namespace support explicitly: see setNamespaceAware.

setValidationModeNameback to summary
public void setValidationModeName(String validationModeName)

Set the validation mode to use by name. Defaults to VALIDATION_AUTO.

See Also
setValidationMode