public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader
BeanDefinitionDocumentReader
interface.
Typically applied to a
DefaultListableBeanFactory
or a 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
BeanDefinitionRegistry
interface.
setDocumentReaderClass(java.lang.Class<? extends org.springframework.beans.factory.xml.BeanDefinitionDocumentReader>)
,
BeanDefinitionDocumentReader
,
DefaultBeanDefinitionDocumentReader
,
BeanDefinitionRegistry
,
DefaultListableBeanFactory
,
GenericApplicationContext
Modifier and Type | Field and Description |
---|---|
static int |
VALIDATION_AUTO
Indicates that the validation mode should be detected automatically.
|
static int |
VALIDATION_DTD
Indicates that DTD validation should be used.
|
static int |
VALIDATION_NONE
Indicates that the validation should be disabled.
|
static int |
VALIDATION_XSD
Indicates that XSD validation should be used.
|
logger
Constructor and Description |
---|
XmlBeanDefinitionReader(BeanDefinitionRegistry registry)
Create new XmlBeanDefinitionReader for the given bean factory.
|
Modifier and Type | Method 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. |
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 |
doLoadBeanDefinitions(org.xml.sax.InputSource inputSource,
Resource resource)
Actually load bean definitions from the specified XML file.
|
protected org.w3c.dom.Document |
doLoadDocument(org.xml.sax.InputSource inputSource,
Resource resource)
Actually load the specified document using the configured DocumentLoader.
|
protected org.xml.sax.EntityResolver |
getEntityResolver()
Return the EntityResolver to use, building a default resolver
if none specified.
|
NamespaceHandlerResolver |
getNamespaceHandlerResolver()
Lazily create a default NamespaceHandlerResolver, if not set before.
|
int |
getValidationMode()
Return the validation mode to use.
|
protected int |
getValidationModeForResource(Resource resource)
Determine the validation mode for the specified
Resource . |
boolean |
isNamespaceAware()
Return whether or not the XML parser should be XML namespace aware.
|
int |
loadBeanDefinitions(EncodedResource encodedResource)
Load bean definitions from the specified XML file.
|
int |
loadBeanDefinitions(org.xml.sax.InputSource inputSource)
Load bean definitions from the specified XML file.
|
int |
loadBeanDefinitions(org.xml.sax.InputSource inputSource,
java.lang.String resourceDescription)
Load bean definitions from the specified XML file.
|
int |
loadBeanDefinitions(Resource resource)
Load bean definitions from the specified XML file.
|
int |
registerBeanDefinitions(org.w3c.dom.Document doc,
Resource resource)
Register the bean definitions contained in the given DOM document.
|
void |
setDocumentLoader(DocumentLoader documentLoader)
Specify the
DocumentLoader to use. |
void |
setDocumentReaderClass(java.lang.Class<? extends BeanDefinitionDocumentReader> documentReaderClass)
Specify the
BeanDefinitionDocumentReader implementation to use,
responsible for the actual reading of the XML bean definition document. |
void |
setEntityResolver(org.xml.sax.EntityResolver entityResolver)
Set a SAX entity resolver to be used for parsing.
|
void |
setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
Set an implementation of the
org.xml.sax.ErrorHandler
interface for custom handling of XML parsing errors and warnings. |
void |
setEventListener(ReaderEventListener eventListener)
Specify which
ReaderEventListener to use. |
void |
setNamespaceAware(boolean namespaceAware)
Set whether or not the XML parser should be XML namespace aware.
|
void |
setNamespaceHandlerResolver(NamespaceHandlerResolver namespaceHandlerResolver)
Specify the
NamespaceHandlerResolver to use. |
void |
setProblemReporter(ProblemReporter problemReporter)
Specify which
ProblemReporter to use. |
void |
setSourceExtractor(SourceExtractor sourceExtractor)
Specify the
SourceExtractor to use. |
void |
setValidating(boolean validating)
Set whether to use XML validation.
|
void |
setValidationMode(int validationMode)
Set the validation mode to use.
|
void |
setValidationModeName(java.lang.String validationModeName)
Set the validation mode to use by name.
|
getBeanClassLoader, getBeanFactory, getBeanNameGenerator, getEnvironment, getRegistry, getResourceLoader, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, setBeanClassLoader, setBeanNameGenerator, setEnvironment, setResourceLoader
public static final int VALIDATION_NONE
public static final int VALIDATION_AUTO
public static final int VALIDATION_DTD
public static final int VALIDATION_XSD
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry)
registry
- the BeanFactory to load bean definitions into,
in the form of a BeanDefinitionRegistrypublic void setValidating(boolean validating)
true
.
This method switches namespace awareness on if validation is turned off, in order to still process schema namespaces properly in such a scenario.
setValidationMode(int)
,
setNamespaceAware(boolean)
public void setValidationModeName(java.lang.String validationModeName)
VALIDATION_AUTO
.setValidationMode(int)
public void setValidationMode(int validationMode)
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(boolean)
.
public int getValidationMode()
public void setNamespaceAware(boolean namespaceAware)
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.
public boolean isNamespaceAware()
public void setProblemReporter(@Nullable ProblemReporter problemReporter)
ProblemReporter
to use.
The default implementation is FailFastProblemReporter
which exhibits fail fast behaviour. External tools can provide an alternative implementation
that collates errors and warnings for display in the tool UI.
public void setEventListener(@Nullable ReaderEventListener eventListener)
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.
public void setSourceExtractor(@Nullable SourceExtractor sourceExtractor)
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.
public void setNamespaceHandlerResolver(@Nullable NamespaceHandlerResolver namespaceHandlerResolver)
NamespaceHandlerResolver
to use.
If none is specified, a default instance will be created through
createDefaultNamespaceHandlerResolver()
.
public void setDocumentLoader(@Nullable DocumentLoader documentLoader)
DocumentLoader
to use.
The default implementation is DefaultDocumentLoader
which loads Document
instances using JAXP.
public void setEntityResolver(@Nullable org.xml.sax.EntityResolver entityResolver)
By default, ResourceEntityResolver
will be used. Can be overridden
for custom entity resolution, for example relative to some specific base path.
protected org.xml.sax.EntityResolver getEntityResolver()
public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
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.
SimpleSaxErrorHandler
public void setDocumentReaderClass(java.lang.Class<? extends BeanDefinitionDocumentReader> documentReaderClass)
BeanDefinitionDocumentReader
implementation to use,
responsible for the actual reading of the XML bean definition document.
The default is DefaultBeanDefinitionDocumentReader
.
documentReaderClass
- the desired BeanDefinitionDocumentReader implementation classpublic int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException
resource
- the resource descriptor for the XML fileBeanDefinitionStoreException
- in case of loading or parsing errorspublic int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException
encodedResource
- the resource descriptor for the XML file,
allowing to specify an encoding to use for parsing the fileBeanDefinitionStoreException
- in case of loading or parsing errorspublic int loadBeanDefinitions(org.xml.sax.InputSource inputSource) throws BeanDefinitionStoreException
inputSource
- the SAX InputSource to read fromBeanDefinitionStoreException
- in case of loading or parsing errorspublic int loadBeanDefinitions(org.xml.sax.InputSource inputSource, @Nullable java.lang.String resourceDescription) throws BeanDefinitionStoreException
inputSource
- the SAX InputSource to read fromresourceDescription
- a description of the resource
(can be null
or empty)BeanDefinitionStoreException
- in case of loading or parsing errorsprotected int doLoadBeanDefinitions(org.xml.sax.InputSource inputSource, Resource resource) throws BeanDefinitionStoreException
inputSource
- the SAX InputSource to read fromresource
- the resource descriptor for the XML fileBeanDefinitionStoreException
- in case of loading or parsing errorsdoLoadDocument(org.xml.sax.InputSource, org.springframework.core.io.Resource)
,
registerBeanDefinitions(org.w3c.dom.Document, org.springframework.core.io.Resource)
protected org.w3c.dom.Document doLoadDocument(org.xml.sax.InputSource inputSource, Resource resource) throws java.lang.Exception
inputSource
- the SAX InputSource to read fromresource
- the resource descriptor for the XML filejava.lang.Exception
- when thrown from the DocumentLoadersetDocumentLoader(org.springframework.beans.factory.xml.DocumentLoader)
,
DocumentLoader.loadDocument(org.xml.sax.InputSource, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, int, boolean)
protected int getValidationModeForResource(Resource resource)
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.
protected int detectValidationMode(Resource resource)
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.
public int registerBeanDefinitions(org.w3c.dom.Document doc, Resource resource) throws BeanDefinitionStoreException
loadBeanDefinitions
.
Creates a new instance of the parser class and invokes
registerBeanDefinitions
on it.
doc
- the DOM documentresource
- the resource descriptor (for context information)BeanDefinitionStoreException
- in case of parsing errorsloadBeanDefinitions(org.springframework.core.io.Resource)
,
setDocumentReaderClass(java.lang.Class<? extends org.springframework.beans.factory.xml.BeanDefinitionDocumentReader>)
,
BeanDefinitionDocumentReader.registerBeanDefinitions(org.w3c.dom.Document, org.springframework.beans.factory.xml.XmlReaderContext)
protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader()
BeanDefinitionDocumentReader
to use for actually
reading bean definitions from an XML document.
The default implementation instantiates the specified "documentReaderClass".
public XmlReaderContext createReaderContext(Resource resource)
XmlReaderContext
to pass over to the document reader.public NamespaceHandlerResolver getNamespaceHandlerResolver()
createDefaultNamespaceHandlerResolver()
protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver()
NamespaceHandlerResolver
used if none is specified.
The default implementation returns an instance of DefaultNamespaceHandlerResolver
.