Package grails.spring
Class BeanBuilder
java.lang.Object
groovy.lang.GroovyObjectSupport
grails.spring.BeanBuilder
- All Implemented Interfaces:
groovy.lang.GroovyObject
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
- Since:
- 0.4
-
Constructor Summary
ConstructorsConstructorDescriptionBeanBuilder(ClassLoader classLoader) BeanBuilder(org.springframework.context.ApplicationContext parent) BeanBuilder(org.springframework.context.ApplicationContext parent, ClassLoader classLoader) BeanBuilder(org.springframework.context.ApplicationContext parentCtx, RuntimeSpringConfiguration springConfig, ClassLoader classLoader) -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanaddToDeferred(BeanConfiguration beanConfig, String property, Object newValue) org.springframework.beans.factory.support.AbstractBeanDefinitionDefines an inner bean definition.org.springframework.beans.factory.support.AbstractBeanDefinitionDefines an inner bean definition.beans(groovy.lang.Closure<?> c) Defines a set of beans for the given block or closure.org.springframework.context.ApplicationContextCreates an ApplicationContext from the current state of the BeanBuilderprotected grails.spring.DynamicElementReadercreateDynamicElementReader(String namespace, boolean decorator) protected RuntimeSpringConfigurationcreateRuntimeSpringConfiguration(org.springframework.context.ApplicationContext parent, ClassLoader cl) protected voidfilterGStringReferences(Object[] constructorArgs) protected voidorg.springframework.beans.factory.config.BeanDefinitiongetBeanDefinition(String name) Retrieves a BeanDefinition for the given nameRetrieves all BeanDefinitions for this BeanBuilderorg.apache.commons.logging.LoggetLog()org.springframework.context.ApplicationContextRetrieves the parent ApplicationContextgetProperty(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 itselfRetrieves the RuntimeSpringConfiguration instance used the the BeanBuildervoidimportBeans(String resourcePattern) Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instancevoidimportBeans(org.springframework.core.io.Resource resource) protected voidinitializeBeanBuilderForClassLoader(ClassLoader classLoader) protected voidprotected BeanBuilderinvokeBeanDefiningClosure(groovy.lang.Closure<?> callable) When an method's argument is only a closure it is a set of bean definitions.protected BeanConfigurationinvokeBeanDefiningMethod(String name, Object[] args) Called when a bean definition node is called.invokeMethod(String name, Object arg) Overrides method invocation to create beans for each method name that takes a class argument.voidTakes 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")voidloadBeans(org.springframework.core.io.Resource resource) Loads a single Resource into the bean buildervoidloadBeans(org.springframework.core.io.Resource[] resources) Loads a set of given beansprotected ObjectmanageListIfNecessary(Object value) Checks whether there are any runtime refs inside the list and converts it to a ManagedList if necessary.protected ObjectmanageMapIfNecessary(Object value) Checks whether there are any runtime refs inside a Map and converts it to a ManagedMap if necessary.voidregisterBeans(RuntimeSpringConfiguration targetSpringConfig) Registers bean definitions with another instance of RuntimeSpringConfiguration, overriding any beans in the target.voidregisterBeans(org.springframework.beans.factory.support.BeanDefinitionRegistry registry) Register a set of beans with the given bean registry.protected ListresolveConstructorArguments(Object[] args, int start, int end) voidsetBinding(groovy.lang.Binding b) Sets the binding (the variables available in the scope of the BeanBuilder).voidsetClassLoader(ClassLoader classLoader) voidsetNamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver) voidsetProperty(String name, Object value) Overrides property setting in the scope of the BeanBuilder to set properties on the current BeanConfiguration.protected voidsetPropertyOnBeanConfig(String name, Object value) voidsetResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver) voidsetSpringConfig(RuntimeSpringConfiguration springConfig) Sets the runtime Spring configuration instance to use.protected Object[]voidDefines a Spring namespace definition to use.Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
-
Constructor Details
-
BeanBuilder
public BeanBuilder() -
BeanBuilder
-
BeanBuilder
public BeanBuilder(org.springframework.context.ApplicationContext parent) -
BeanBuilder
-
BeanBuilder
public BeanBuilder(org.springframework.context.ApplicationContext parentCtx, RuntimeSpringConfiguration springConfig, ClassLoader classLoader)
-
-
Method Details
-
setResourcePatternResolver
public void setResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver) -
initializeSpringConfig
protected void initializeSpringConfig() -
setClassLoader
-
initializeBeanBuilderForClassLoader
-
setNamespaceHandlerResolver
public void setNamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver) -
createRuntimeSpringConfiguration
protected RuntimeSpringConfiguration createRuntimeSpringConfiguration(org.springframework.context.ApplicationContext parent, ClassLoader cl) -
getLog
public org.apache.commons.logging.Log getLog() -
importBeans
Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance- Parameters:
resourcePattern- The resource pattern
-
importBeans
public void importBeans(org.springframework.core.io.Resource resource) -
xmlns
Defines a Spring namespace definition to use.- Parameters:
definition- The definition
-
getParentCtx
public org.springframework.context.ApplicationContext getParentCtx()Retrieves the parent ApplicationContext- Returns:
- The parent ApplicationContext
-
getSpringConfig
Retrieves the RuntimeSpringConfiguration instance used the the BeanBuilder- Returns:
- The RuntimeSpringConfiguration instance
-
getBeanDefinition
Retrieves a BeanDefinition for the given name- Parameters:
name- The bean definition- Returns:
- The BeanDefinition instance
-
getBeanDefinitions
Retrieves all BeanDefinitions for this BeanBuilder- Returns:
- A map of BeanDefinition instances with the bean id as the key
-
setSpringConfig
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
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(org.springframework.core.io.Resource resource) Loads a single Resource into the bean builder- Parameters:
resource- The resource to load
-
loadBeans
public void loadBeans(org.springframework.core.io.Resource[] resources) 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(org.springframework.beans.factory.support.BeanDefinitionRegistry registry) Register a set of beans with the given bean registry. Most application contexts are bean registries. -
registerBeans
Registers bean definitions with another instance of RuntimeSpringConfiguration, overriding any beans in the target.- Parameters:
targetSpringConfig- The RuntimeSpringConfiguration object
-
invokeMethod
Overrides method invocation to create beans for each method name that takes a class argument. -
beans
Defines a set of beans for the given block or closure.- Parameters:
c- The block or closure- Returns:
- This BeanBuilder instance
-
createApplicationContext
public org.springframework.context.ApplicationContext createApplicationContext()Creates an ApplicationContext from the current state of the BeanBuilder- Returns:
- The ApplicationContext instance
-
finalizeDeferredProperties
protected void finalizeDeferredProperties() -
addToDeferred
-
invokeBeanDefiningMethod
Called when a bean definition node is called.- Parameters:
name- The name of the bean to defineargs- 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
-
resolveConstructorArguments
-
subarray
-
filterGStringReferences
-
invokeBeanDefiningClosure
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
-
setProperty
Overrides property setting in the scope of the BeanBuilder to set properties on the current BeanConfiguration. -
bean
Defines an inner bean definition.- Parameters:
type- The bean type- Returns:
- The bean definition
-
bean
public org.springframework.beans.factory.support.AbstractBeanDefinition bean(Class type, Object... args) Defines an inner bean definition.- Parameters:
type- The bean typeargs- The constructors arguments and closure configurer- Returns:
- The bean definition
-
setPropertyOnBeanConfig
-
manageMapIfNecessary
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
-
manageListIfNecessary
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
-
getProperty
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 -
createDynamicElementReader
protected grails.spring.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
-