Announcement Announcement Module
No announcement yet.
XML entities in application context Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML entities in application context

    I have an application context defined using XML config files with the Spring 2.5 XSD, and would like to use XML entities to provide an extension point for customizing a client's installation.

    I started with an XML config file defined in the customary fashion:

    <beans xmlns=""
      <!-- Define beans here... -->
    Now, I would like to define some entities that will be resolved using a filesystem resource. A couple of Google searches suggest adding the following inline DTD snippet:

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" ""
    <!ENTITY % entities SYSTEM "my/path/entities.ent" >
    The problem I'm running into is that the Spring 2.5 XSD referenced in my document root seems to be disregarded in favor of the more restrictive DTD, and the SAX parser no longer accepts my config file as valid XML. I get exceptions such as:

    org.xml.sax.SAXParseException: Attribute "xmlns" must be declared for element type "beans".

    I need the more flexible XSD-based validation because I'm using elements from the Spring Security namespace that are not allowed by the simple DTD. Any suggestions on how to get DTD-based XML entity declarations to co-exist with the Spring 2.5 XSD?

  • #2
    OK, upon closer inspection it's a JAXP DOM parser and not a SAX parser. It turns out that XmlBeanDefinitionReader attempts to guess the validation mode by peeking at the config file contents. If it sees a DTD DOCTYPE declaration prior to the first opening tag, it configures the DOM parser to use DTD-based validation; otherwise, it uses XSD-based validation. You can change this behavior using the XmlBeanDefinitionReader.setValidationMode(int) method.

    In order to set the validation behavior when initializing my web application context, I found it necessary to extend the default XmlWebApplicationContext class:

    package com.example;
    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    public class MyApplicationContext extends XmlWebApplicationContext {
      public MyApplicationContext() {
      protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
    And then add a context parameter to my web.xml to override the default application context with the new custom context class: