Class DefaultRuntimeSpringConfiguration

  • All Implemented Interfaces:
    RuntimeSpringConfiguration

    public class DefaultRuntimeSpringConfiguration
    extends java.lang.Object
    implements RuntimeSpringConfiguration
    A programmable runtime Spring configuration that allows a spring ApplicationContext to be constructed at runtime. Credit must go to Solomon Duskis and the article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
    Since:
    0.3
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<java.lang.String,​java.util.List<java.lang.String>> aliases  
      protected org.springframework.beans.factory.ListableBeanFactory beanFactory  
      protected java.lang.ClassLoader classLoader  
      protected org.springframework.context.support.GenericApplicationContext context  
      protected org.springframework.context.ApplicationContext parent  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      BeanConfiguration addAbstractBean​(java.lang.String name)
      \ Adds an abstract bean definition to the bean factory and returns the BeanConfiguration object.
      void addAlias​(java.lang.String alias, java.lang.String beanName)
      Adds an alias to a given bean name.
      void addBeanConfiguration​(java.lang.String beanName, BeanConfiguration beanConfiguration)
      Adds a bean configuration to the list of beans to be created.
      void addBeanDefinition​(java.lang.String name, org.springframework.beans.factory.config.BeanDefinition bd)
      Adds a Spring BeanDefinition.
      BeanConfiguration addPrototypeBean​(java.lang.String name)
      Adds an empty prototype bean configuration.
      BeanConfiguration addPrototypeBean​(java.lang.String name, java.lang.Class clazz)
      Adds a prototype bean definition.
      BeanConfiguration addSingletonBean​(java.lang.String name)
      Adds an empty singleton bean configuration.
      BeanConfiguration addSingletonBean​(java.lang.String name, java.lang.Class clazz)
      Adds a singleton bean definition.
      BeanConfiguration addSingletonBean​(java.lang.String name, java.lang.Class clazz, java.util.Collection args)
      Creates a new singleton bean and adds it to the list of bean references.
      boolean containsBean​(java.lang.String name)
      Returns whether the runtime spring config contains the specified bean.
      protected org.springframework.context.support.GenericApplicationContext createApplicationContext​(org.springframework.context.ApplicationContext parentCtx)
      Creates the ApplicationContext instance.
      org.springframework.beans.factory.support.AbstractBeanDefinition createBeanDefinition​(java.lang.String name)
      Creates and returns the BeanDefinition that is regsitered within the given name or returns null.
      BeanConfiguration createPrototypeBean​(java.lang.String name)
      Creates a new prototype bean configuration.
      BeanConfiguration createSingletonBean​(java.lang.Class clazz)
      Creates a singleton bean configuration.
      BeanConfiguration createSingletonBean​(java.lang.Class clazz, java.util.Collection constructorArguments)
      Creates a singleton bean configuration.
      BeanConfiguration createSingletonBean​(java.lang.String name)
      Creates a new singleton bean configuration.
      org.springframework.context.ApplicationContext getApplicationContext()
      Retrieves the application context from the current state.
      BeanConfiguration getBeanConfig​(java.lang.String name)
      Returns the BeanConfiguration for the specified name.
      org.springframework.beans.factory.config.BeanDefinition getBeanDefinition​(java.lang.String beanName)
      Obtains a BeanDefinition instance for the given beanName.
      java.util.List<java.lang.String> getBeanNames()  
      org.springframework.context.ApplicationContext getUnrefreshedApplicationContext()  
      protected void initialiseApplicationContext()
      Initialises the ApplicationContext instance.
      void registerBeansWithConfig​(RuntimeSpringConfiguration targetSpringConfig)
      Registers the beans held within this RuntimeSpringConfiguration instance with the given RuntimeSpringConfiguration.
      void registerBeansWithContext​(org.springframework.context.support.GenericApplicationContext applicationContext)
      Registers the beans held within this RuntimeSpringConfiguration instance with the given ApplicationContext.
      void registerBeansWithRegistry​(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)
      Registers the beans held within this RuntimeSpringConfiguration instance with the given BeanDefinitionRegistry.
      void registerPostProcessor​(org.springframework.beans.factory.config.BeanFactoryPostProcessor processor)
      Registers a bean factory post processor with the context.
      void setBeanFactory​(org.springframework.beans.factory.ListableBeanFactory beanFactory)
      Sets the BeanFactory implementation to use.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • context

        protected org.springframework.context.support.GenericApplicationContext context
      • parent

        protected org.springframework.context.ApplicationContext parent
      • classLoader

        protected java.lang.ClassLoader classLoader
      • aliases

        protected java.util.Map<java.lang.String,​java.util.List<java.lang.String>> aliases
      • beanFactory

        protected org.springframework.beans.factory.ListableBeanFactory beanFactory
    • Constructor Detail

      • DefaultRuntimeSpringConfiguration

        public DefaultRuntimeSpringConfiguration()
      • DefaultRuntimeSpringConfiguration

        public DefaultRuntimeSpringConfiguration​(org.springframework.context.ApplicationContext parent)
      • DefaultRuntimeSpringConfiguration

        public DefaultRuntimeSpringConfiguration​(org.springframework.context.ApplicationContext parent,
                                                 java.lang.ClassLoader cl)
    • Method Detail

      • createApplicationContext

        protected org.springframework.context.support.GenericApplicationContext createApplicationContext​(org.springframework.context.ApplicationContext parentCtx)
        Creates the ApplicationContext instance. Subclasses can override to customise the used ApplicationContext
        Parameters:
        parentCtx - The parent ApplicationContext instance. Can be null.
        Returns:
        An instance of GenericApplicationContext
      • initialiseApplicationContext

        protected void initialiseApplicationContext()
        Initialises the ApplicationContext instance.
      • createSingletonBean

        public BeanConfiguration createSingletonBean​(java.lang.Class clazz)
        Description copied from interface: RuntimeSpringConfiguration
        Creates a singleton bean configuration. Differs from addSingletonBean in that it doesn't add the bean to the list of bean references. Hence should be used for creating nested beans.
        Specified by:
        createSingletonBean in interface RuntimeSpringConfiguration
        Returns:
        A BeanConfiguration instance
      • addSingletonBean

        public BeanConfiguration addSingletonBean​(java.lang.String name,
                                                  java.lang.Class clazz,
                                                  java.util.Collection args)
        Description copied from interface: RuntimeSpringConfiguration
        Creates a new singleton bean and adds it to the list of bean references.
        Specified by:
        addSingletonBean in interface RuntimeSpringConfiguration
        Parameters:
        name - The name of the bean
        clazz - The class of the bean
        args - The constructor arguments of the bean
        Returns:
        A BeanConfiguration instance
      • createSingletonBean

        public BeanConfiguration createSingletonBean​(java.lang.Class clazz,
                                                     java.util.Collection constructorArguments)
        Description copied from interface: RuntimeSpringConfiguration
        Creates a singleton bean configuration. Differs from addSingletonBean in that it doesn't add the bean to the list of bean references. Hence should be used for creating nested beans
        Specified by:
        createSingletonBean in interface RuntimeSpringConfiguration
        Parameters:
        clazz - The bean class
        constructorArguments - The constructor arguments
        Returns:
        A BeanConfiguration instance
      • createPrototypeBean

        public BeanConfiguration createPrototypeBean​(java.lang.String name)
        Description copied from interface: RuntimeSpringConfiguration
        Creates a new prototype bean configuration. Differs from addPrototypeBean in that it doesn't add the bean to the list of bean references to be created via the getApplicationContext() method, hence can be used for creating nested beans
        Specified by:
        createPrototypeBean in interface RuntimeSpringConfiguration
        Parameters:
        name - The bean name
        Returns:
        A BeanConfiguration instance
      • createSingletonBean

        public BeanConfiguration createSingletonBean​(java.lang.String name)
        Description copied from interface: RuntimeSpringConfiguration
        Creates a new singleton bean configuration. Differs from addSingletonBean in that it doesn't add the bean to the list of bean references to be created via the getApplicationContext() method, hence can be used for creating nested beans
        Specified by:
        createSingletonBean in interface RuntimeSpringConfiguration
        Parameters:
        name - The bean name
        Returns:
        A BeanConfiguration instance
      • addBeanDefinition

        public void addBeanDefinition​(java.lang.String name,
                                      org.springframework.beans.factory.config.BeanDefinition bd)
        Description copied from interface: RuntimeSpringConfiguration
        Adds a Spring BeanDefinition. Differs from BeanConfiguration which is a factory class for creating BeanDefinition instances
        Specified by:
        addBeanDefinition in interface RuntimeSpringConfiguration
        Parameters:
        name - The name of the bean
        bd - The BeanDefinition instance
      • containsBean

        public boolean containsBean​(java.lang.String name)
        Description copied from interface: RuntimeSpringConfiguration
        Returns whether the runtime spring config contains the specified bean.
        Specified by:
        containsBean in interface RuntimeSpringConfiguration
        Parameters:
        name - The bean name
        Returns:
        true if it does
      • createBeanDefinition

        public org.springframework.beans.factory.support.AbstractBeanDefinition createBeanDefinition​(java.lang.String name)
        Description copied from interface: RuntimeSpringConfiguration
        Creates and returns the BeanDefinition that is regsitered within the given name or returns null.
        Specified by:
        createBeanDefinition in interface RuntimeSpringConfiguration
        Parameters:
        name - The name of the bean definition
        Returns:
        A BeanDefinition
      • registerPostProcessor

        public void registerPostProcessor​(org.springframework.beans.factory.config.BeanFactoryPostProcessor processor)
        Description copied from interface: RuntimeSpringConfiguration
        Registers a bean factory post processor with the context.
        Specified by:
        registerPostProcessor in interface RuntimeSpringConfiguration
        Parameters:
        processor - The BeanFactoryPostProcessor instance
      • registerBeansWithContext

        public void registerBeansWithContext​(org.springframework.context.support.GenericApplicationContext applicationContext)
        Description copied from interface: RuntimeSpringConfiguration
        Registers the beans held within this RuntimeSpringConfiguration instance with the given ApplicationContext.
        Specified by:
        registerBeansWithContext in interface RuntimeSpringConfiguration
        Parameters:
        applicationContext - The ApplicationContext instance
      • registerBeansWithRegistry

        public void registerBeansWithRegistry​(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)
        Description copied from interface: RuntimeSpringConfiguration
        Registers the beans held within this RuntimeSpringConfiguration instance with the given BeanDefinitionRegistry.
        Specified by:
        registerBeansWithRegistry in interface RuntimeSpringConfiguration
        Parameters:
        registry - The BeanDefinitionRegistry instance
      • addAlias

        public void addAlias​(java.lang.String alias,
                             java.lang.String beanName)
        Description copied from interface: RuntimeSpringConfiguration
        Adds an alias to a given bean name.
        Specified by:
        addAlias in interface RuntimeSpringConfiguration
        Parameters:
        alias - The alias
        beanName - The bean
      • getBeanDefinition

        public org.springframework.beans.factory.config.BeanDefinition getBeanDefinition​(java.lang.String beanName)
        Description copied from interface: RuntimeSpringConfiguration
        Obtains a BeanDefinition instance for the given beanName.
        Specified by:
        getBeanDefinition in interface RuntimeSpringConfiguration
        Parameters:
        beanName - The beanName
        Returns:
        The BeanDefinition or null if it doesn't exit
      • setBeanFactory

        public void setBeanFactory​(org.springframework.beans.factory.ListableBeanFactory beanFactory)
        Description copied from interface: RuntimeSpringConfiguration
        Sets the BeanFactory implementation to use.
        Specified by:
        setBeanFactory in interface RuntimeSpringConfiguration
        Parameters:
        beanFactory - The BeanFactory implementation