Browse Source

AbstractBeanDefinitionParser allows for skipping evaluation of XML "name" attribute

Issue: SPR-12643
pull/722/merge
Juergen Hoeller 10 years ago
parent
commit
11bf3b3524
  1. 6
      spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionReaderUtils.java
  2. 30
      spring-beans/src/main/java/org/springframework/beans/factory/xml/AbstractBeanDefinitionParser.java

6
spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionReaderUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -150,8 +150,8 @@ public class BeanDefinitionReaderUtils {
// Register aliases for bean name, if any. // Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases(); String[] aliases = definitionHolder.getAliases();
if (aliases != null) { if (aliases != null) {
for (String aliase : aliases) { for (String alias : aliases) {
registry.registerAlias(beanName, aliase); registry.registerAlias(beanName, alias);
} }
} }
} }

30
spring-beans/src/main/java/org/springframework/beans/factory/xml/AbstractBeanDefinitionParser.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,12 +49,13 @@ import org.springframework.util.StringUtils;
*/ */
public abstract class AbstractBeanDefinitionParser implements BeanDefinitionParser { public abstract class AbstractBeanDefinitionParser implements BeanDefinitionParser {
/** Constant for the id attribute */ /** Constant for the "id" attribute */
public static final String ID_ATTRIBUTE = "id"; public static final String ID_ATTRIBUTE = "id";
/** Constant for the name attribute */ /** Constant for the "name" attribute */
public static final String NAME_ATTRIBUTE = "name"; public static final String NAME_ATTRIBUTE = "name";
@Override @Override
public final BeanDefinition parse(Element element, ParserContext parserContext) { public final BeanDefinition parse(Element element, ParserContext parserContext) {
AbstractBeanDefinition definition = parseInternal(element, parserContext); AbstractBeanDefinition definition = parseInternal(element, parserContext);
@ -66,10 +67,12 @@ public abstract class AbstractBeanDefinitionParser implements BeanDefinitionPars
"Id is required for element '" + parserContext.getDelegate().getLocalName(element) "Id is required for element '" + parserContext.getDelegate().getLocalName(element)
+ "' when used as a top-level tag", element); + "' when used as a top-level tag", element);
} }
String[] aliases = new String[0]; String[] aliases = null;
String name = element.getAttribute(NAME_ATTRIBUTE); if (shouldParseNameAsAliases()) {
if (StringUtils.hasLength(name)) { String name = element.getAttribute(NAME_ATTRIBUTE);
aliases = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name)); if (StringUtils.hasLength(name)) {
aliases = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
}
} }
BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id, aliases); BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id, aliases);
registerBeanDefinition(holder, parserContext.getRegistry()); registerBeanDefinition(holder, parserContext.getRegistry());
@ -170,7 +173,18 @@ public abstract class AbstractBeanDefinitionParser implements BeanDefinitionPars
} }
/** /**
* Controls whether this parser is supposed to fire a * Determine whether the element's "name" attribute should get parsed as
* bean definition aliases, i.e. alternative bean definition names.
* <p>The default implementation returns {@code true}.
* @return whether the parser should evaluate the "name" attribute as aliases
* @since 4.1.5
*/
protected boolean shouldParseNameAsAliases() {
return true;
}
/**
* Determine whether this parser is supposed to fire a
* {@link org.springframework.beans.factory.parsing.BeanComponentDefinition} * {@link org.springframework.beans.factory.parsing.BeanComponentDefinition}
* event after parsing the bean definition. * event after parsing the bean definition.
* <p>This implementation returns {@code true} by default; that is, * <p>This implementation returns {@code true} by default; that is,

Loading…
Cancel
Save