Package: grails.spring

## [Java] Class BeanBuilder

- grails.spring.BeanBuilder

-     public class BeanBuilder
      extends groovy.lang.GroovyObjectSupport

  Runtime bean configuration wrapper. Like a Groovy builder, but more of a DSL for Spring configuration. Allows syntax like:

       import org.hibernate.SessionFactory
       import org.apache.tomcat.jdbc.pool.DataSource

       BeanBuilder builder = new BeanBuilder()
       builder.beans {
         dataSource(DataSource) {                  // <--- invokeMethod
            driverClassName = "org.h2.Driver"
            url = "jdbc:h2:mem:grailsDB"
            username = "sa"                            // <-- setProperty
            password = ""
            settings = [mynew:"setting"]
        }
        sessionFactory(SessionFactory) {
               dataSource = dataSource                 // <-- getProperty for retrieving refs
        }
        myService(MyService) {
            nestedBean = { AnotherBean bean->          // <-- setProperty with closure for nested bean
                    dataSource = dataSource
            }
        }
       }

  You can also use the Spring IO API to load resources containing beans defined as a Groovy script using either the constructors or the loadBeans(Resource[] resources) method

  Authors:
    Graeme Rocher
  Since:
    0.4

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `private class` | `BeanBuilder.ConfigurableRuntimeBeanReference` | Adds new properties to runtime references.
    `private class` | `BeanBuilder.DeferredProperty` | Defers the adding of a property to a bean definition until later.

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `private static java.lang.String` | `BEANS` |
    `private static java.lang.String` | `CREATE_APPCTX` |
    `private static org.apache.commons.logging.Log` | `LOG` |
    `private static java.lang.String` | `REF` |
    `private static java.lang.String` | `REGISTER_BEANS` |
    `private org.springframework.core.io.Resource` | `beanBuildResource` |
    `private java.util.Map<java.lang.String, java.lang.Object>` | `binding` |
    `private java.lang.ClassLoader` | `classLoader` |
    `private BeanConfiguration` | `currentBeanConfig` |
    `private java.util.Map<java.lang.String, DeferredProperty>` | `deferredProperties` |
    `private org.springframework.beans.factory.xml.NamespaceHandlerResolver` | `namespaceHandlerResolver` |
    `private java.util.Map<java.lang.String, org.springframework.beans.factory.xml.NamespaceHandler>` | `namespaceHandlers` |
    `private java.util.Map<java.lang.String, java.lang.String>` | `namespaces` |
    `private org.springframework.context.ApplicationContext` | `parentCtx` |
    `private org.springframework.beans.factory.xml.XmlReaderContext` | `readerContext` |
    `private org.springframework.core.io.support.ResourcePatternResolver` | `resourcePatternResolver` |
    `private static long` | `serialVersionUID` |
    `private RuntimeSpringConfiguration` | `springConfig` |
    `private org.springframework.beans.factory.xml.XmlBeanDefinitionReader` | `xmlBeanDefinitionReader` |

  - ### Constructor Summary

    Constructors
    Constructor and description
    `BeanBuilder ()`
    `BeanBuilder (java.lang.ClassLoader classLoader)`
    `BeanBuilder (org.springframework.context.ApplicationContext parent)`
    `BeanBuilder (org.springframework.context.ApplicationContext parent, java.lang.ClassLoader classLoader)`
    `BeanBuilder (org.springframework.context.ApplicationContext parentCtx, RuntimeSpringConfiguration springConfig, java.lang.ClassLoader classLoader)`

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected boolean` | `addToDeferred(BeanConfiguration beanConfig, java.lang.String property, java.lang.Object newValue)`
    `` | `protected void` | `afterInvocation()`
    `` | `public org.springframework.beans.factory.support.AbstractBeanDefinition` | `bean(java.lang.Class<?> type)` Defines an inner bean definition.
    `` | `public org.springframework.beans.factory.support.AbstractBeanDefinition` | `bean(java.lang.Class type, java.lang.Object args)` Defines an inner bean definition.
    `` | `public BeanBuilder` | `beans(groovy.lang.Closure<?> c)` Defines a set of beans for the given block or closure.
    `` | `public java.lang.Object` | `call(java.lang.Object args)`
    `` | `public org.springframework.context.ApplicationContext` | `createApplicationContext()` Creates an ApplicationContext from the current state of the BeanBuilder
    `` | `protected DynamicElementReader` | `createDynamicElementReader(java.lang.String namespace, boolean decorator)`
    `` | `protected RuntimeSpringConfiguration` | `createRuntimeSpringConfiguration(org.springframework.context.ApplicationContext parent, java.lang.ClassLoader cl)`
    `` | `protected void` | `filterGStringReferences(java.lang.Object[] constructorArgs)`
    `` | `protected void` | `finalizeDeferredProperties()`
    `` | `public org.springframework.beans.factory.config.BeanDefinition` | `getBeanDefinition(java.lang.String name)` Retrieves a BeanDefinition for the given name
    `` | `public java.util.Map<java.lang.String, org.springframework.beans.factory.config.BeanDefinition>` | `getBeanDefinitions()` Retrieves all BeanDefinitions for this BeanBuilder
    `` | `public org.apache.commons.logging.Log` | `getLog()`
    `` | `public org.springframework.context.ApplicationContext` | `getParentCtx()` Retrieves the parent ApplicationContext
    `` | `public java.lang.Object` | `getProperty(java.lang.String name)` Overrides property retrieval in the scope of the BeanBuilder to either:
    `` | `public RuntimeSpringConfiguration` | `getSpringConfig()` Retrieves the RuntimeSpringConfiguration instance used the the BeanBuilder
    `` | `public void` | `importBeans(java.lang.String resourcePattern)` Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance
    `` | `public void` | `importBeans(org.springframework.core.io.Resource resource)`
    `` | `protected void` | `initializeBeanBuilderForClassLoader(java.lang.ClassLoader classLoader)`
    `` | `protected void` | `initializeSpringConfig()`
    `` | `protected BeanBuilder` | `invokeBeanDefiningClosure(groovy.lang.Closure<?> callable)` When an method's argument is only a closure it is a set of bean definitions.
    `` | `protected BeanConfiguration` | `invokeBeanDefiningMethod(java.lang.String name, java.lang.Object[] args)` Called when a bean definition node is called.
    `` | `public java.lang.Object` | `invokeMethod(java.lang.String name, java.lang.Object arg)` Overrides method invocation to create beans for each method name that takes a class argument.
    `` | `public void` | `loadBeans(java.lang.String resourcePattern)` Takes a resource pattern as ( See Also: org.springframework.core.io.support.PathMatchingResourcePatternResolver
    `` | `public void` | `loadBeans(org.springframework.core.io.Resource resource)` Loads a single Resource into the bean builder
    `` | `public void` | `loadBeans(org.springframework.core.io.Resource[] resources)` Loads a set of given beans
    `` | `protected java.lang.Object` | `manageListIfNecessary(java.lang.Object value)` Checks whether there are any runtime refs inside the list and converts it to a ManagedList if necessary.
    `` | `protected java.lang.Object` | `manageMapIfNecessary(java.lang.Object value)` Checks whether there are any runtime refs inside a Map and converts it to a ManagedMap if necessary.
    `` | `public void` | `registerBeans(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)` Register a set of beans with the given bean registry.
    `` | `public void` | `registerBeans(RuntimeSpringConfiguration targetSpringConfig)` Registers bean definitions with another instance of RuntimeSpringConfiguration, overriding any beans in the target.
    `` | `protected java.util.List` | `resolveConstructorArguments(java.lang.Object[] args, int start, int end)`
    `` | `public void` | `setBinding(groovy.lang.Binding b)` Sets the binding (the variables available in the scope of the BeanBuilder).
    `` | `public void` | `setClassLoader(java.lang.ClassLoader classLoader)`
    `` | `public void` | `setNamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver)`
    `` | `public void` | `setProperty(java.lang.String name, java.lang.Object value)` Overrides property setting in the scope of the BeanBuilder to set properties on the current BeanConfiguration.
    `` | `protected void` | `setPropertyOnBeanConfig(java.lang.String name, java.lang.Object value)`
    `` | `public void` | `setResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver)`
    `` | `public void` | `setSpringConfig(RuntimeSpringConfiguration springConfig)` Sets the runtime Spring configuration instance to use.
    `` | `public void` | `setVariable(java.lang.String name, java.lang.Object value)`
    `` | `protected java.lang.Object[]` | `subarray(java.lang.Object[] args, int i, int j)`
    `` | `public void` | `xmlns(java.util.Map<java.lang.String, java.lang.String> definition)` Defines a Spring namespace definition to use.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class groovy.lang.GroovyObjectSupport` | `groovy.lang.GroovyObjectSupport#getMetaClass(), groovy.lang.GroovyObjectSupport#setMetaClass(groovy.lang.MetaClass), groovy.lang.GroovyObjectSupport#wait(long), groovy.lang.GroovyObjectSupport#wait(long, int), groovy.lang.GroovyObjectSupport#wait(), groovy.lang.GroovyObjectSupport#equals(java.lang.Object), groovy.lang.GroovyObjectSupport#toString(), groovy.lang.GroovyObjectSupport#hashCode(), groovy.lang.GroovyObjectSupport#getClass(), groovy.lang.GroovyObjectSupport#notify(), groovy.lang.GroovyObjectSupport#notifyAll(), groovy.lang.GroovyObjectSupport#invokeMethod(java.lang.String, java.lang.Object), groovy.lang.GroovyObjectSupport#getProperty(java.lang.String), groovy.lang.GroovyObjectSupport#setProperty(java.lang.String, java.lang.Object)`

- - ### Field Detail

    - #### private static final java.lang.String BEANS

    - #### private static final java.lang.String CREATE_APPCTX

    - #### private static final org.apache.commons.logging.Log LOG

    - #### private static final java.lang.String REF

    - #### private static final java.lang.String REGISTER_BEANS

    - #### private org.springframework.core.io.Resource beanBuildResource

    - #### private java.util.Map<java.lang.String, java.lang.Object> binding

    - #### private java.lang.ClassLoader classLoader

    - #### private BeanConfiguration currentBeanConfig

    - #### private java.util.Map<java.lang.String, DeferredProperty> deferredProperties

    - #### private org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver

    - #### private java.util.Map<java.lang.String, org.springframework.beans.factory.xml.NamespaceHandler> namespaceHandlers

    - #### private java.util.Map<java.lang.String, java.lang.String> namespaces

    - #### private org.springframework.context.ApplicationContext parentCtx

    - #### private org.springframework.beans.factory.xml.XmlReaderContext readerContext

    - #### private org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver

    - #### private static final long serialVersionUID

    - #### private RuntimeSpringConfiguration springConfig

    - #### private org.springframework.beans.factory.xml.XmlBeanDefinitionReader xmlBeanDefinitionReader

  - ### Constructor Detail

    - #### public BeanBuilder()

    - #### public BeanBuilder(java.lang.ClassLoader classLoader)

    - #### public BeanBuilder(org.springframework.context.ApplicationContext parent)

    - #### public BeanBuilder(org.springframework.context.ApplicationContext parent, java.lang.ClassLoader classLoader)

    - #### public BeanBuilder(org.springframework.context.ApplicationContext parentCtx, RuntimeSpringConfiguration springConfig, java.lang.ClassLoader classLoader)

  - ### Method Detail

    - #### protected boolean addToDeferred(BeanConfiguration beanConfig, java.lang.String property, java.lang.Object newValue)

    - #### @java.lang.Override protected void afterInvocation()

    - #### public org.springframework.beans.factory.support.AbstractBeanDefinition bean(java.lang.Class<?> type)

      Defines an inner bean definition.

      Parameters:
        `type` - The bean type
      Returns:
        The bean definition

    - #### @java.lang.SuppressWarnings("rawtypes") public org.springframework.beans.factory.support.AbstractBeanDefinition bean(java.lang.Class type, java.lang.Object args)

      Defines an inner bean definition.

      Parameters:
        `type` - The bean type
        `args` - The constructors arguments and closure configurer
      Returns:
        The bean definition

    - #### public BeanBuilder beans(groovy.lang.Closure<?> c)

      Defines a set of beans for the given block or closure.

      Parameters:
        `c` - The block or closure
      Returns:
        This BeanBuilder instance

    - #### @java.lang.Override public java.lang.Object call(java.lang.Object args)

    - #### public org.springframework.context.ApplicationContext createApplicationContext()

      Creates an ApplicationContext from the current state of the BeanBuilder

      Returns:
        The ApplicationContext instance

    - #### protected DynamicElementReader createDynamicElementReader(java.lang.String namespace, boolean decorator)

    - #### protected RuntimeSpringConfiguration createRuntimeSpringConfiguration(org.springframework.context.ApplicationContext parent, java.lang.ClassLoader cl)

    - #### protected void filterGStringReferences(java.lang.Object[] constructorArgs)

    - #### protected void finalizeDeferredProperties()

    - #### public org.springframework.beans.factory.config.BeanDefinition getBeanDefinition(java.lang.String name)

      Retrieves a BeanDefinition for the given name

      Parameters:
        `name` - The bean definition
      Returns:
        The BeanDefinition instance

    - #### public java.util.Map<java.lang.String, org.springframework.beans.factory.config.BeanDefinition> getBeanDefinitions()

      Retrieves all BeanDefinitions for this BeanBuilder

      Returns:
        A map of BeanDefinition instances with the bean id as the key

    - #### public org.apache.commons.logging.Log getLog()

    - #### public org.springframework.context.ApplicationContext getParentCtx()

      Retrieves the parent ApplicationContext

      Returns:
        The parent ApplicationContext

    - #### @java.lang.Override public java.lang.Object getProperty(java.lang.String name)

      Overrides property retrieval in the scope of the BeanBuilder to either: a) Retrieve a variable from the bean builder's binding if it exists b) Retrieve a RuntimeBeanReference for a specific bean if it exists c) Otherwise just delegate to super.getProperty which will resolve properties from the BeanBuilder itself

    - #### public RuntimeSpringConfiguration getSpringConfig()

      Retrieves the RuntimeSpringConfiguration instance used the the BeanBuilder

      Returns:
        The RuntimeSpringConfiguration instance

    - #### public void importBeans(java.lang.String resourcePattern)

      Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance

      Parameters:
        `resourcePattern` - The resource pattern

    - #### public void importBeans(org.springframework.core.io.Resource resource)

    - #### protected void initializeBeanBuilderForClassLoader(java.lang.ClassLoader classLoader)

    - #### protected void initializeSpringConfig()

    - #### protected BeanBuilder invokeBeanDefiningClosure(groovy.lang.Closure<?> callable)

      When an method's argument is only a closure it is a set of bean definitions.

      Parameters:
        `callable` - The closure argument
      Returns:
        This BeanBuilder instance

    - #### protected BeanConfiguration invokeBeanDefiningMethod(java.lang.String name, java.lang.Object[] args)

      Called when a bean definition node is called.

      Parameters:
        `name` - The name of the bean to define
        `args` - The arguments to the bean. The first argument is the class name, the last argument is sometimes a closure. All the arguments in between are constructor arguments
      Returns:
        The bean configuration instance

    - #### @java.lang.Override public java.lang.Object invokeMethod(java.lang.String name, java.lang.Object arg)

      Overrides method invocation to create beans for each method name that takes a class argument.

    - #### public void loadBeans(java.lang.String resourcePattern)

      Takes a resource pattern as (

      throws:
        IOException When the path cannot be matched
      See Also:
        org.springframework.core.io.support.PathMatchingResourcePatternResolver
      Parameters:
        `resourcePattern` - The resource pattern

    - #### public void loadBeans(org.springframework.core.io.Resource resource)

      Loads a single Resource into the bean builder

      Parameters:
        `resource` - The resource to load

    - #### public void loadBeans(org.springframework.core.io.Resource[] resources)

      Loads a set of given beans

      throws:
        IOException Thrown if there is an error reading one of the passes resources
      Parameters:
        `resources` - The resources to load

    - #### @java.lang.SuppressWarnings({ "rawtypes", "unchecked" }) protected java.lang.Object manageListIfNecessary(java.lang.Object value)

      Checks whether there are any runtime refs inside the list and converts it to a ManagedList if necessary.

      Parameters:
        `value` - The object that represents the list
      Returns:
        Either a new list or a managed one

    - #### @java.lang.SuppressWarnings({ "rawtypes", "unchecked" }) protected java.lang.Object manageMapIfNecessary(java.lang.Object value)

      Checks whether there are any runtime refs inside a Map and converts it to a ManagedMap if necessary.

      Parameters:
        `value` - The current map
      Returns:
        A ManagedMap or a normal map

    - #### public void registerBeans(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)

      Register a set of beans with the given bean registry. Most application contexts are bean registries.

    - #### public void registerBeans(RuntimeSpringConfiguration targetSpringConfig)

      Registers bean definitions with another instance of RuntimeSpringConfiguration, overriding any beans in the target.

      Parameters:
        `targetSpringConfig` - The RuntimeSpringConfiguration object

    - #### @java.lang.SuppressWarnings("rawtypes") protected java.util.List resolveConstructorArguments(java.lang.Object[] args, int start, int end)

    - #### @java.lang.SuppressWarnings("unchecked") public void setBinding(groovy.lang.Binding b)

      Sets the binding (the variables available in the scope of the BeanBuilder).

      Parameters:
        `b` - The Binding instance

    - #### public void setClassLoader(java.lang.ClassLoader classLoader)

    - #### public void setNamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver)

    - #### @java.lang.Override public void setProperty(java.lang.String name, java.lang.Object value)

      Overrides property setting in the scope of the BeanBuilder to set properties on the current BeanConfiguration.

    - #### protected void setPropertyOnBeanConfig(java.lang.String name, java.lang.Object value)

    - #### public void setResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver)

    - #### public void setSpringConfig(RuntimeSpringConfiguration springConfig)

      Sets the runtime Spring configuration instance to use. This is not necessary to set and is configured to default value if not, but is useful for integrating with other spring configuration mechanisms

      See Also:
        org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator
      Parameters:
        `springConfig` - The spring config

    - #### @java.lang.Override public void setVariable(java.lang.String name, java.lang.Object value)

    - #### protected java.lang.Object[] subarray(java.lang.Object[] args, int i, int j)

    - #### public void xmlns(java.util.Map<java.lang.String, java.lang.String> definition)

      Defines a Spring namespace definition to use.

      Parameters:
        `definition` - The definition