类 AbstractSimpleBeanDefinitionParser
- java.lang.Object
- org.springframework.beans.factory.xml.AbstractBeanDefinitionParser
- org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
- org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser
- 所有已实现的接口:
BeanDefinitionParser
public abstract class AbstractSimpleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser
Convenient base class for when there exists a one-to-one mapping between attribute names on the element that is to be parsed and the property names on theClassbeing configured.Extend this parser class when you want to create a single bean definition from a relatively simple custom XML element. The resulting
BeanDefinitionwill be automatically registered with the relevantBeanDefinitionRegistry.An example will hopefully make the use of this particular parser class immediately clear. Consider the following class definition:
public class SimpleCache implements Cache { public void setName(String name) {...} public void setTimeout(int timeout) {...} public void setEvictionPolicy(EvictionPolicy policy) {...} // remaining class definition elided for clarity... }Then let us assume the following XML tag has been defined to permit the easy configuration of instances of the above class;
<caching:cache name="..." timeout="..." eviction-policy="..."/>
All that is required of the Java developer tasked with writing the parser to parse the above XML tag into an actual
SimpleCachebean definition is the following:public class SimpleCacheBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser { protected Class getBeanClass(Element element) { return SimpleCache.class; } }Please note that the
AbstractSimpleBeanDefinitionParseris limited to populating the created bean definition with property values. if you want to parse constructor arguments and nested elements from the supplied XML element, then you will have to implement thepostProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.w3c.dom.Element)method and do such parsing yourself, or (more likely) subclass theAbstractSingleBeanDefinitionParserorAbstractBeanDefinitionParserclasses directly.The process of actually registering the
SimpleCacheBeanDefinitionParserwith the Spring XML parsing infrastructure is described in the Spring Framework reference documentation (in one of the appendices).For an example of this parser in action (so to speak), do look at the source code for the
UtilNamespaceHandler.PropertiesBeanDefinitionParser; the observant (and even not so observant) reader will immediately notice that there is next to no code in the implementation. ThePropertiesBeanDefinitionParserpopulates aPropertiesFactoryBeanfrom an XML element that looks like this:<util:properties location="jdbc.properties"/>
The observant reader will notice that the sole attribute on the
<util:properties/>element matches thePropertiesLoaderSupport.setLocation(org.springframework.core.io.Resource)method name on thePropertiesFactoryBean(the general usage thus illustrated holds true for any number of attributes). All that thePropertiesBeanDefinitionParserneeds actually do is supply an implementation of theAbstractSingleBeanDefinitionParser.getBeanClass(org.w3c.dom.Element)method to return thePropertiesFactoryBeantype.- 从以下版本开始:
- 2.0
- 作者:
- Rob Harrop, Rick Evans, Juergen Hoeller
- 另请参阅:
Conventions.attributeNameToPropertyName(String)
字段概要
从类继承的字段 org.springframework.beans.factory.xml.AbstractBeanDefinitionParser
ID_ATTRIBUTE, NAME_ATTRIBUTE
构造器概要
构造器 构造器 说明 AbstractSimpleBeanDefinitionParser()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected voiddoParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)Parse the suppliedElementand populate the suppliedBeanDefinitionBuilderas required.protected StringextractPropertyName(String attributeName)Extract a JavaBean property name from the supplied attribute name.protected booleanisEligibleAttribute(String attributeName)Determine whether the given attribute is eligible for being turned into a corresponding bean property value.protected booleanisEligibleAttribute(Attr attribute, ParserContext parserContext)Determine whether the given attribute is eligible for being turned into a corresponding bean property value.protected voidpostProcess(BeanDefinitionBuilder beanDefinition, Element element)Hook method that derived classes can implement to inspect/change a bean definition after parsing is complete.从类继承的方法 org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
doParse, getBeanClass, getBeanClassName, getParentName, parseInternal
从类继承的方法 org.springframework.beans.factory.xml.AbstractBeanDefinitionParser
parse, postProcessComponentDefinition, registerBeanDefinition, resolveId, shouldFireEvents, shouldGenerateId, shouldGenerateIdAsFallback, shouldParseNameAsAliases
构造器详细资料
AbstractSimpleBeanDefinitionParser
public AbstractSimpleBeanDefinitionParser()
方法详细资料
doParse
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
Parse the suppliedElementand populate the suppliedBeanDefinitionBuilderas required.This implementation maps any attributes present on the supplied element to
PropertyValueinstances, andadds themto thebuilder.The
extractPropertyName(String)method is used to reconcile the name of an attribute with the name of a JavaBean property.- 覆盖:
doParse在类中AbstractSingleBeanDefinitionParser- 参数:
element- the XML element being parsedbuilder- used to define theBeanDefinitionparserContext- the object encapsulating the current state of the parsing process- 另请参阅:
extractPropertyName(String)
isEligibleAttribute
protected boolean isEligibleAttribute(Attr attribute, ParserContext parserContext)
Determine whether the given attribute is eligible for being turned into a corresponding bean property value.The default implementation considers any attribute as eligible, except for the "id" attribute and namespace declaration attributes.
- 参数:
attribute- the XML attribute to checkparserContext- theParserContext- 另请参阅:
isEligibleAttribute(String)
isEligibleAttribute
protected boolean isEligibleAttribute(String attributeName)
Determine whether the given attribute is eligible for being turned into a corresponding bean property value.The default implementation considers any attribute as eligible, except for the "id" attribute.
- 参数:
attributeName- the attribute name taken straight from the XML element being parsed (nevernull)
extractPropertyName
protected String extractPropertyName(String attributeName)
Extract a JavaBean property name from the supplied attribute name.The default implementation uses the
Conventions.attributeNameToPropertyName(String)method to perform the extraction.The name returned must obey the standard JavaBean property name conventions. For example for a class with a setter method '
setBingoHallFavourite(String)', the name returned had better be 'bingoHallFavourite' (with that exact casing).- 参数:
attributeName- the attribute name taken straight from the XML element being parsed (nevernull)- 返回:
- the extracted JavaBean property name (must never be
null)
postProcess
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element)
Hook method that derived classes can implement to inspect/change a bean definition after parsing is complete.The default implementation does nothing.
- 参数:
beanDefinition- the parsed (and probably totally defined) bean definition being builtelement- the XML element that was the source of the bean definition's metadata