Announcement Announcement Module
Collapse
No announcement yet.
'id' attribute required for Extensible XML Authoring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 'id' attribute required for Extensible XML Authoring

    We're using Spring 2.5.6 and have developed a a custom schema as described in 'Appendix B: Extensible XML Authoring'

    We have implemented a number of custom elements and things are working great.

    However, we would like to define a global element without specifying an 'id' attribute. In our custom XSD we have:

    Code:
    <xsd:element name="customBeanPostProcessor"/>
    We want this in our Spring XML configuration file as:

    Code:
    <ns:customBeanPostProcessor/>
    without specifying an 'id' attribute. However when we do, we get an BeanDefinitionParsingException

    Code:
    "Configuration problem: Id is required for element 'customBeanPostProcessor' when used as a top-level tag"
    How can we define a custom element without requiring an 'id' attribute in the Spring application context XML file?

    Thanks in advance

  • #2
    I've had the same problem and I've coded a workaround (I don't know of an official way to do it).

    The idea is to automatically provide Spring with an id, if the user does not give one. This is done by overriding the method "resolveId" of class AbstractBeanDefinitionParser, which is used to parse the id of the current bean.

    Hope that helps

    Code:
    public class MyBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
    
    	protected Class getBeanClass(Element element) {
    		return MyBean.class;
    	}
    	private static int n = 0;
    	protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
    					throws BeanDefinitionStoreException {
    		String id = super.resolveId(element, definition, parserContext);
    		if (StringUtils.hasText(id))
    			return id;
    		return "autoGeneratedPropertyBeanId_" + n++;
    	}
    }

    Comment


    • #3
      Excellent, thanks Pablo. That led me in the right direction.

      Comment


      • #4
        I believe the right way to do this is:

        @Override
        protected boolean shouldGenerateId() {
        return true;
        }

        Comment

        Working...
        X