Class AbstractSpringPoweredDigesterFactory

All Implemented Interfaces:
org.apache.commons.digester3.ObjectCreationFactory<Object>, IDigesterRuleAware, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware
Direct Known Subclasses:
ConfigurationDigesterFactory, GenericFactory, JmsRealmsFactory, MonitoringFactory, SharedResourceFactory

public abstract class AbstractSpringPoweredDigesterFactory extends org.apache.commons.digester3.AbstractObjectCreationFactory<Object> implements org.springframework.context.ApplicationContextAware, IDigesterRuleAware
This is a factory for objects to be used with the 'factory-create-rule' of the Apache Digester framework as a replacement for the 'object-create-rule'. The intention is to have objects created by the Apache Digester be created via the Spring Factory thus allowing for dependency injection; and if not possible then create them ourselves but inject at least a reference to the Spring Factory when supported by the object. When the object is created directly by this factory, the Spring Factory is used for auto-wiring and initialization. The factory is abstract; subclasses will need to implement method 'getSuggestedBeanName()' to return the name of the default Bean to load from the Spring Context. All Beans defined in the Spring Context that can be loaded via this Factory must be defined as Prototype beans (scope="prototype") because the expected behaviour from an ObjectCreationFactory is to create new instances with each call. The Apache Digester will normally set extra properties for each instance. (See isPrototypesOnly().)
Tim van der Leeuw
  • Field Details

    • log

      protected org.apache.logging.log4j.Logger log
  • Constructor Details

    • AbstractSpringPoweredDigesterFactory

      protected AbstractSpringPoweredDigesterFactory()
  • Method Details

    • getSuggestedBeanName

      public abstract String getSuggestedBeanName()
      Suggest the name of the bean which should be retrieved from the Spring BeanFactory. If a className attribute has also been specified in the XML, then that takes precedence over finding a bean with given suggestedBeanName. If for the className multiple bean-definitions are found in the factory, then a bean is selected from those with this given suggestedBeanName. If no such bean exists, an error is thrown because the factory can not select between multiple beans.
    • isPrototypesOnly

      public boolean isPrototypesOnly()
      Return true is only prototype beans from the Spring Context will be returned, false is a Spring singleton bean might be returned. This is hard-coded to return true only in this class. If a subclass wishes to allow using singleton-beans, then this method should be overridden.
    • createObject

      public Object createObject(Attributes attrs) throws Exception
      Create object using, if possible, the Spring BeanFactory. An object is created according to the following rules:
      1. If no attribute 'className' is given in the configuration file, then the bean named with method getSuggestedBeanName() is created from the Spring context.
      2. If exactly 1 bean of type given by 'className' attribute can be found in the Spring context, an instance of that bean is created from the Spring factory.
        The value returned by method getSuggestedBeanName() is, in this case, not relevant.
      3. If multiple beans of type given by 'className' attribute are defined in the Spring context, then an instance is created whose bean-name is the same as that returned by the method getSuggestedBeanName().
      4. If the Spring context contains no beans of type 'className', then a new instance of this class is created without accessing the Spring factory.
        The Spring BeanFactory will then be invoked to attempt auto-wiring beans by name and initialization via any BeanFactory - callback methods. If the created class implements interface BeanFactoryAware, the Spring factory will be made available as a property so that it can be accessed directly from the bean.
        (NB:Objects created by the Spring Factory will also have a pointer to the creating BeanFactory when they implement this interface.)
      Specified by:
      createObject in interface org.apache.commons.digester3.ObjectCreationFactory<Object>
      Specified by:
      createObject in class org.apache.commons.digester3.AbstractObjectCreationFactory<Object>
      See Also:
      • org.apache.commons.digester.ObjectCreationFactory#createObject(org.xml.sax.Attributes)
    • createObject

      protected Object createObject(Map<String,String> attrs) throws ClassNotFoundException
      Create Object from Spring factory, but using the attributes from the XML converted to a Map. This is so that sub-classes can override this method and change attributes in the map before creating the object from the Spring factory.
    • createBeanAndAutoWire

      protected <T> T createBeanAndAutoWire(Class<T> beanClass)
    • copyAttrsToMap

      protected Map<String,String> copyAttrsToMap(Attributes attrs)
    • setDigesterRule

      public final void setDigesterRule(DigesterRule rule)
      Specified by:
      setDigesterRule in interface IDigesterRuleAware
    • getApplicationContext

      public org.springframework.context.ApplicationContext getApplicationContext()
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware