The most simple usage of Spring is to load xml definitions beans and use it.
We will go through the whole work flow based on spring unittest.
Go and check XmlBeanDefinitionReaderTests from package org.springframework.beans.factory.xml,
The method "withImport" show the simple workflow to load beans.
Load document
We will go through the whole work flow based on spring unittest.
Go and check XmlBeanDefinitionReaderTests from package org.springframework.beans.factory.xml,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void withImport() { | |
SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); | |
Resource resource = new ClassPathResource("import.xml", getClass()); | |
new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); | |
testBeanDefinitions(registry); | |
} |
The method "withImport" show the simple workflow to load beans.
Let’s jump inside loadBeanDefinitions. The overview workflow is compromised by 3 steps as following:
Validate Mode for resource.
The first main part is validate mode for resource.
As we know, XML had 2 type of validation: DTD VS XSD. The first step is check the file content to detect which mode we will parse the XML file.
Load document
Convert the input stream to xml document.
Register Bean Definition
CreateDelegate: Return BeanDefinitaionParseDelegate class. The actual implement class which parse the xml content and create Spring bean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans profile="dev"> | |
<bean id="devDatasourceConfig" | |
class="org.baeldung.profiles.DevDatasourceConfig" /> | |
</beans> |
Before parse the actual bean, checking the profile at first. The working env should accept the profile of process defined in xml file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (this.delegate.isDefaultNamespace(root)) { | |
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE); | |
if (StringUtils.hasText(profileSpec)) { | |
String[] specifiedProfiles = StringUtils.tokenizeToStringArray( | |
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS); | |
// We cannot use Profiles.of(...) since profile expressions are not supported | |
// in XML config. See SPR-12458 for details. | |
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) { | |
if (logger.isDebugEnabled()) { | |
logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec + | |
"] not matching: " + getReaderContext().getResource()); | |
} | |
return; | |
} | |
} | |
} |
After that, we finally reach the core function parseBeanDefinitions(root, this.delegate) to load beans. Let’s dive more into the this method.
The simple logic here is that the code visit all children of root element if spring default name space or parseCustomElement for customized spring definition. parseDefaultElement define the detailed behavior to parse.
BeanDefinitionParserDelegate: Stateful delegate class used to parse XML bean definitions.
Delegate.isDefaultNamespace: “http://www.springframework.org/schema/beans" or empty. The bean definition is the regular spring bean definition.
ParseDefaultElement will handle 4 types of spring beans .
- Import — importBeanDefinitionResource
- Alias — parseAliasRegistration
- Bean — parseBeanDefinition
- beans — doRegisterBeanDifinitions
- parseCustomElement will parse customized bean definition.
Comments
Post a Comment