📄 defaultxmlbeandefinitionparser.java
字号:
// just apply default to singletons, as lazy-init has no meaning for prototypes
lazyInit = this.defaultLazyInit;
}
bd.setLazyInit(TRUE_VALUE.equals(lazyInit));
return bd;
}
catch (ClassNotFoundException ex) {
throw new BeanDefinitionStoreException(
this.resource, beanName, "Bean class [" + className + "] not found", ex);
}
catch (NoClassDefFoundError err) {
throw new BeanDefinitionStoreException(
this.resource, beanName, "Class that bean class [" + className + "] depends on not found", err);
}
}
/**
* Parse constructor argument subelements of the given bean element.
*/
protected ConstructorArgumentValues getConstructorArgSubElements(Element beanEle, String beanName)
throws ClassNotFoundException {
NodeList nl = beanEle.getChildNodes();
ConstructorArgumentValues cargs = new ConstructorArgumentValues();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && CONSTRUCTOR_ARG_ELEMENT.equals(node.getNodeName())) {
parseConstructorArgElement((Element) node, beanName, cargs);
}
}
return cargs;
}
/**
* Parse property value subelements of the given bean element.
*/
protected MutablePropertyValues getPropertyValueSubElements(Element beanEle, String beanName) {
NodeList nl = beanEle.getChildNodes();
MutablePropertyValues pvs = new MutablePropertyValues();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && PROPERTY_ELEMENT.equals(node.getNodeName())) {
parsePropertyElement((Element) node, beanName, pvs);
}
}
return pvs;
}
/**
* Parse lookup-override sub elements.
*/
protected void getLookupOverrideSubElements(Element beanEle, String beanName, MethodOverrides overrides) {
NodeList nl = beanEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && LOOKUP_METHOD_ELEMENT.equals(node.getNodeName())) {
Element ele = (Element) node;
String methodName = ele.getAttribute(NAME_ATTRIBUTE);
String beanRef = ele.getAttribute(BEAN_ELEMENT);
overrides.addOverride(new LookupOverride(methodName, beanRef));
}
}
}
protected void getReplacedMethodSubElements(Element beanEle, String beanName, MethodOverrides overrides) {
NodeList nl = beanEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && REPLACED_METHOD_ELEMENT.equals(node.getNodeName())) {
Element replacedMethodEle = (Element) node;
String name = replacedMethodEle.getAttribute(NAME_ATTRIBUTE);
String callback = replacedMethodEle.getAttribute(REPLACER_ATTRIBUTE);
ReplaceOverride replaceOverride = new ReplaceOverride(name, callback);
// Look for arg-type match elements
NodeList argTypeNodes = replacedMethodEle.getElementsByTagName(ARG_TYPE_ELEMENT);
for (int j = 0; j < argTypeNodes.getLength(); j++) {
Element argTypeEle = (Element) argTypeNodes.item(j);
replaceOverride.addTypeIdentifier(argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE));
}
overrides.addOverride(replaceOverride);
}
}
}
/**
* Parse a constructor-arg element.
*/
protected void parseConstructorArgElement(Element ele, String beanName, ConstructorArgumentValues cargs)
throws DOMException, ClassNotFoundException {
Object val = getPropertyValue(ele, beanName, null);
String indexAttr = ele.getAttribute(INDEX_ATTRIBUTE);
String typeAttr = ele.getAttribute(TYPE_ATTRIBUTE);
if (StringUtils.hasLength(indexAttr)) {
try {
int index = Integer.parseInt(indexAttr);
if (index < 0) {
throw new BeanDefinitionStoreException(this.resource, beanName, "'index' cannot be lower than 0");
}
if (StringUtils.hasLength(typeAttr)) {
cargs.addIndexedArgumentValue(index, val, typeAttr);
}
else {
cargs.addIndexedArgumentValue(index, val);
}
}
catch (NumberFormatException ex) {
throw new BeanDefinitionStoreException(this.resource, beanName,
"Attribute 'index' of tag 'constructor-arg' must be an integer");
}
}
else {
if (StringUtils.hasLength(typeAttr)) {
cargs.addGenericArgumentValue(val, typeAttr);
}
else {
cargs.addGenericArgumentValue(val);
}
}
}
/**
* Parse a property element.
*/
protected void parsePropertyElement(Element ele, String beanName, MutablePropertyValues pvs) {
String propertyName = ele.getAttribute(NAME_ATTRIBUTE);
if (!StringUtils.hasLength(propertyName)) {
throw new BeanDefinitionStoreException(
this.resource, beanName, "Tag 'property' must have a 'name' attribute");
}
if (pvs.contains(propertyName)) {
throw new BeanDefinitionStoreException(
this.resource, beanName, "Multiple 'property' definitions for property '" + propertyName + "'");
}
Object val = getPropertyValue(ele, beanName, propertyName);
pvs.addPropertyValue(propertyName, val);
}
/**
* Get the value of a property element. May be a list etc.
*/
protected Object getPropertyValue(Element ele, String beanName, String propertyName) {
// should only have one element child: value, ref, collection
NodeList nl = ele.getChildNodes();
Element valueRefOrCollectionElement = null;
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i) instanceof Element) {
Element candidateEle = (Element) nl.item(i);
if (DESCRIPTION_ELEMENT.equals(candidateEle.getTagName())) {
// keep going: we don't use this value for now
}
else {
// child element is what we're looking for
valueRefOrCollectionElement = candidateEle;
}
}
}
if (valueRefOrCollectionElement == null) {
String elementName = (propertyName != null) ?
"<property> element for property '" + propertyName + "'" :
"<constructor-arg> element";
throw new BeanDefinitionStoreException(
this.resource, beanName, elementName + " must have a subelement like <value> or <ref>");
}
return parsePropertySubelement(valueRefOrCollectionElement, beanName);
}
/**
* Parse a value, ref or collection subelement of a property element
* @param ele subelement of property element; we don't know which yet
*/
protected Object parsePropertySubelement(Element ele, String beanName) {
if (ele.getTagName().equals(BEAN_ELEMENT)) {
return parseBeanDefinition(ele);
}
else if (ele.getTagName().equals(REF_ELEMENT)) {
// a generic reference to any name of any bean
String beanRef = ele.getAttribute(BEAN_REF_ATTRIBUTE);
if (!StringUtils.hasLength(beanRef)) {
// a reference to the id of another bean in the same XML file
beanRef = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
if (!StringUtils.hasLength(beanRef)) {
// a reference to the id of another bean in the same XML file
beanRef = ele.getAttribute(PARENT_REF_ATTRIBUTE);
if (!StringUtils.hasLength(beanRef)) {
throw new BeanDefinitionStoreException(
this.resource, beanName, "'bean', 'local' or 'parent' is required for a reference");
}
return new RuntimeBeanReference(beanRef, true);
}
}
return new RuntimeBeanReference(beanRef);
}
else if (ele.getTagName().equals(IDREF_ELEMENT)) {
// a generic reference to any name of any bean
String beanRef = ele.getAttribute(BEAN_REF_ATTRIBUTE);
if (!StringUtils.hasLength(beanRef)) {
// a reference to the id of another bean in the same XML file
beanRef = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
if (!StringUtils.hasLength(beanRef)) {
throw new BeanDefinitionStoreException(
this.resource, beanName, "Either 'bean' or 'local' is required for an idref");
}
}
return beanRef;
}
else if (ele.getTagName().equals(LIST_ELEMENT)) {
return getList(ele, beanName);
}
else if (ele.getTagName().equals(SET_ELEMENT)) {
return getSet(ele, beanName);
}
else if (ele.getTagName().equals(MAP_ELEMENT)) {
return getMap(ele, beanName);
}
else if (ele.getTagName().equals(PROPS_ELEMENT)) {
return getProps(ele, beanName);
}
else if (ele.getTagName().equals(VALUE_ELEMENT)) {
// it's a literal value
return getTextValue(ele, beanName);
}
else if (ele.getTagName().equals(NULL_ELEMENT)) {
// it's a distinguished null value
return null;
}
throw new BeanDefinitionStoreException(
this.resource, beanName, "Unknown subelement of <property>: <" + ele.getTagName() + ">");
}
protected List getList(Element collectionEle, String beanName) {
NodeList nl = collectionEle.getChildNodes();
ManagedList list = new ManagedList(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i) instanceof Element) {
Element ele = (Element) nl.item(i);
list.add(parsePropertySubelement(ele, beanName));
}
}
return list;
}
protected Set getSet(Element collectionEle, String beanName) {
NodeList nl = collectionEle.getChildNodes();
ManagedSet set = new ManagedSet(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i) instanceof Element) {
Element ele = (Element) nl.item(i);
set.add(parsePropertySubelement(ele, beanName));
}
}
return set;
}
protected Map getMap(Element mapEle, String beanName) {
List list = getChildElementsByTagName(mapEle, ENTRY_ELEMENT);
Map map = new ManagedMap(list.size());
for (int i = 0; i < list.size(); i++) {
Element entryEle = (Element) list.get(i);
String key = entryEle.getAttribute(KEY_ATTRIBUTE);
// TODO hack: make more robust
NodeList subEles = entryEle.getElementsByTagName("*");
map.put(key, parsePropertySubelement((Element) subEles.item(0), beanName));
}
return map;
}
/**
* Don't use the horrible DOM API to get child elements:
* Get an element's children with a given element name
*/
protected List getChildElementsByTagName(Element mapEle, String elementName) {
NodeList nl = mapEle.getChildNodes();
List nodes = new ArrayList();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n instanceof Element && elementName.equals(n.getNodeName())) {
nodes.add(n);
}
}
return nodes;
}
protected Properties getProps(Element propsEle, String beanName) {
Properties props = new Properties();
NodeList nl = propsEle.getElementsByTagName(PROP_ELEMENT);
for (int i = 0; i < nl.getLength(); i++) {
Element propEle = (Element) nl.item(i);
String key = propEle.getAttribute(KEY_ATTRIBUTE);
// trim the text value to avoid unwanted whitespace
// caused by typical XML formatting
String value = getTextValue(propEle, beanName).trim();
props.setProperty(key, value);
}
return props;
}
/**
* Make the horrible DOM API slightly more bearable:
* get the text value we know this element contains.
*/
protected String getTextValue(Element ele, String beanName) {
StringBuffer value = new StringBuffer();
NodeList nl = ele.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if (item instanceof org.w3c.dom.CharacterData) {
if (!(item instanceof Comment)) {
value.append(item.getNodeValue());
}
}
else {
throw new BeanDefinitionStoreException(
this.resource, beanName,
"<value> element is just allowed to have text and comment nodes, not: " + item.getClass().getName());
}
}
return value.toString();
}
protected int getDependencyCheck(String att) {
int dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE;
if (DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE.equals(att)) {
dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_ALL;
}
else if (DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE.equals(att)) {
dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE;
}
else if (DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE.equals(att)) {
dependencyCheckCode = AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS;
}
// else leave default value
return dependencyCheckCode;
}
protected int getAutowireMode(String att) {
int autowire = AbstractBeanDefinition.AUTOWIRE_NO;
if (AUTOWIRE_BY_NAME_VALUE.equals(att)) {
autowire = AbstractBeanDefinition.AUTOWIRE_BY_NAME;
}
else if (AUTOWIRE_BY_TYPE_VALUE.equals(att)) {
autowire = AbstractBeanDefinition.AUTOWIRE_BY_TYPE;
}
else if (AUTOWIRE_CONSTRUCTOR_VALUE.equals(att)) {
autowire = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR;
}
else if (AUTOWIRE_AUTODETECT_VALUE.equals(att)) {
autowire = AbstractBeanDefinition.AUTOWIRE_AUTODETECT;
}
// else leave default value
return autowire;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -