grails.spring
Class BeanBuilder

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by grails.spring.BeanBuilder
All Implemented Interfaces:
groovy.lang.GroovyObject
Direct Known Subclasses:
WebBeanBuilder

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.commons.dbcp.BasicDataSource
 
 BeanBuilder builder = new BeanBuilder()
 builder.beans {
   dataSource(BasicDataSource) {                  // <--- invokeMethod
      driverClassName = "org.hsqldb.jdbcDriver"
      url = "jdbc:hsqldb: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

Since:
0.4
Author:
Graeme Rocher

Constructor Summary
BeanBuilder()
           
BeanBuilder(ApplicationContext parent)
           
BeanBuilder(ApplicationContext parent, ClassLoader classLoader)
           
BeanBuilder(ApplicationContext parentCtx, RuntimeSpringConfiguration springConfig, ClassLoader classLoader)
           
BeanBuilder(ClassLoader classLoader)
           
 
Method Summary
 BeanBuilder beans(groovy.lang.Closure c)
          Defines a set of beans for the given block or closure.
 ApplicationContext createApplicationContext()
          Creates an ApplicationContext from the current state of the BeanBuilder
protected  DynamicElementReader createDynamicElementReader(String namespace, boolean decorator)
           
protected  RuntimeSpringConfiguration createRuntimeSpringConfiguration(ApplicationContext parent, ClassLoader classLoader)
           
 BeanDefinition getBeanDefinition(String name)
          Retrieves a BeanDefinition for the given name
 Map<String,BeanDefinition> getBeanDefinitions()
          Retrieves all BeanDefinitions for this BeanBuilder
 org.apache.commons.logging.Log getLog()
           
 ApplicationContext getParentCtx()
          Retrieves the parent ApplicationContext
 Object getProperty(String name)
          This method 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
 RuntimeSpringConfiguration getSpringConfig()
          Retrieves the RuntimeSpringConfiguration instance used the the BeanBuilder
 Object invokeMethod(String name, Object arg)
          This method overrides method invocation to create beans for each method name that takes a class argument
 void loadBeans(Resource resource)
          Loads a single Resource into the bean builder
 void loadBeans(Resource[] resources)
          Loads a set of given beans
 void loadBeans(String resourcePattern)
          Takes a resource pattern as (@see org.springframework.core.io.support.PathMatchingResourcePatternResolver) This allows you load multiple bean resources in this single builder eg loadBeans("classpath:*Beans.groovy")
 void registerBeans(BeanDefinitionRegistry registry)
          Register a set of beans with the given bean registry.
 void registerBeans(RuntimeSpringConfiguration targetSpringConfig)
          Registers bean definitions with another instance of RuntimeSpringConfiguration, overriding any beans in the target.
 void setBinding(groovy.lang.Binding b)
          Sets the binding (the variables available in the scope of the BeanBuilder)
 void setClassLoader(ClassLoader classLoader)
           
 void setNamespaceHandlerResolver(NamespaceHandlerResolver namespaceHandlerResolver)
           
 void setProperty(String name, Object value)
          This method overrides property setting in the scope of the BeanBuilder to set properties on the current BeanConfiguration
 void setSpringConfig(RuntimeSpringConfiguration springConfig)
          Sets the runtime Spring configuration instance to use.
 void xmlns(Map<String,String> definition)
          Defines an Spring namespace definition to use
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanBuilder

public BeanBuilder()

BeanBuilder

public BeanBuilder(ClassLoader classLoader)

BeanBuilder

public BeanBuilder(ApplicationContext parent)

BeanBuilder

public BeanBuilder(ApplicationContext parent,
                   ClassLoader classLoader)

BeanBuilder

public BeanBuilder(ApplicationContext parentCtx,
                   RuntimeSpringConfiguration springConfig,
                   ClassLoader classLoader)
Method Detail

setClassLoader

public void setClassLoader(ClassLoader classLoader)

setNamespaceHandlerResolver

public void setNamespaceHandlerResolver(NamespaceHandlerResolver namespaceHandlerResolver)

createRuntimeSpringConfiguration

protected RuntimeSpringConfiguration createRuntimeSpringConfiguration(ApplicationContext parent,
                                                                      ClassLoader classLoader)

getLog

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

xmlns

public void xmlns(Map<String,String> definition)
Defines an Spring namespace definition to use

Parameters:
definition - The definition

getParentCtx

public ApplicationContext getParentCtx()
Retrieves the parent ApplicationContext

Returns:
The parent ApplicationContext

getSpringConfig

public RuntimeSpringConfiguration getSpringConfig()
Retrieves the RuntimeSpringConfiguration instance used the the BeanBuilder

Returns:
The RuntimeSpringConfiguration instance

getBeanDefinition

public BeanDefinition getBeanDefinition(String name)
Retrieves a BeanDefinition for the given name

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

getBeanDefinitions

public Map<String,BeanDefinition> getBeanDefinitions()
Retrieves all BeanDefinitions for this BeanBuilder

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

setSpringConfig

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 org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator

Parameters:
springConfig - The spring config

loadBeans

public void loadBeans(String resourcePattern)
               throws IOException
Takes a resource pattern as (@see org.springframework.core.io.support.PathMatchingResourcePatternResolver) This allows you load multiple bean resources in this single builder eg loadBeans("classpath:*Beans.groovy")

Parameters:
resourcePattern - The resource pattern
Throws:
IOException - When the path cannot be matched

loadBeans

public void loadBeans(Resource resource)
               throws IOException
Loads a single Resource into the bean builder

Parameters:
resource - The resource to load
Throws:
IOException - When an error occurs

loadBeans

public void loadBeans(Resource[] resources)
               throws IOException
Loads a set of given beans

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

registerBeans

public void registerBeans(BeanDefinitionRegistry registry)
Register a set of beans with the given bean registry. Most application contexts are bean registries.


registerBeans

public void registerBeans(RuntimeSpringConfiguration targetSpringConfig)
Registers bean definitions with another instance of RuntimeSpringConfiguration, overriding any beans in the target.

Parameters:
targetSpringConfig - The RuntimeSpringConfiguration object

invokeMethod

public Object invokeMethod(String name,
                           Object arg)
This method overrides method invocation to create beans for each method name that takes a class argument

Specified by:
invokeMethod in interface groovy.lang.GroovyObject
Overrides:
invokeMethod in class groovy.lang.GroovyObjectSupport

beans

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

createApplicationContext

public ApplicationContext createApplicationContext()
Creates an ApplicationContext from the current state of the BeanBuilder

Returns:
The ApplicationContext instance

setProperty

public void setProperty(String name,
                        Object value)
This method overrides property setting in the scope of the BeanBuilder to set properties on the current BeanConfiguration

Specified by:
setProperty in interface groovy.lang.GroovyObject
Overrides:
setProperty in class groovy.lang.GroovyObjectSupport

getProperty

public Object getProperty(String name)
This method 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

Specified by:
getProperty in interface groovy.lang.GroovyObject
Overrides:
getProperty in class groovy.lang.GroovyObjectSupport

createDynamicElementReader

protected DynamicElementReader createDynamicElementReader(String namespace,
                                                          boolean decorator)

setBinding

public void setBinding(groovy.lang.Binding b)
Sets the binding (the variables available in the scope of the BeanBuilder)

Parameters:
b - The Binding instance


Copyright (c) 2005-2009 The Grails project