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
| Constructor and Description |
|---|
BeanBuilder() |
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) |
BeanBuilder(java.lang.ClassLoader classLoader) |
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
addToDeferred(BeanConfiguration beanConfig,
java.lang.String property,
java.lang.Object newValue) |
org.springframework.beans.factory.support.AbstractBeanDefinition |
bean(java.lang.Class<?> type)
Defines an inner bean definition.
|
org.springframework.beans.factory.support.AbstractBeanDefinition |
bean(java.lang.Class type,
java.lang.Object... args)
Defines an inner bean definition.
|
BeanBuilder |
beans(groovy.lang.Closure<?> c)
Defines a set of beans for the given block or closure.
|
org.springframework.context.ApplicationContext |
createApplicationContext()
Creates an ApplicationContext from the current state of the BeanBuilder
|
protected grails.spring.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() |
org.springframework.beans.factory.config.BeanDefinition |
getBeanDefinition(java.lang.String name)
Retrieves a BeanDefinition for the given name
|
java.util.Map<java.lang.String,org.springframework.beans.factory.config.BeanDefinition> |
getBeanDefinitions()
Retrieves all BeanDefinitions for this BeanBuilder
|
org.apache.commons.logging.Log |
getLog() |
org.springframework.context.ApplicationContext |
getParentCtx()
Retrieves the parent ApplicationContext
|
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
|
RuntimeSpringConfiguration |
getSpringConfig()
Retrieves the RuntimeSpringConfiguration instance used the the BeanBuilder
|
void |
importBeans(java.lang.String resourcePattern)
Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance
|
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.
|
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.
|
void |
loadBeans(org.springframework.core.io.Resource resource)
Loads a single Resource into the bean builder
|
void |
loadBeans(org.springframework.core.io.Resource[] resources)
Loads a set of given beans
|
void |
loadBeans(java.lang.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")
|
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.
|
void |
registerBeans(org.springframework.beans.factory.support.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.
|
protected java.util.List |
resolveConstructorArguments(java.lang.Object[] args,
int start,
int end) |
void |
setBinding(groovy.lang.Binding b)
Sets the binding (the variables available in the scope of the BeanBuilder).
|
void |
setClassLoader(java.lang.ClassLoader classLoader) |
void |
setNamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver) |
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) |
void |
setResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver) |
void |
setSpringConfig(RuntimeSpringConfiguration springConfig)
Sets the runtime Spring configuration instance to use.
|
protected java.lang.Object[] |
subarray(java.lang.Object[] args,
int i,
int j) |
void |
xmlns(java.util.Map<java.lang.String,java.lang.String> definition)
Defines a Spring namespace definition to use.
|
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)
public void setResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver)
protected void initializeSpringConfig()
public void setClassLoader(java.lang.ClassLoader classLoader)
protected void initializeBeanBuilderForClassLoader(java.lang.ClassLoader classLoader)
public void setNamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver namespaceHandlerResolver)
protected RuntimeSpringConfiguration createRuntimeSpringConfiguration(org.springframework.context.ApplicationContext parent, java.lang.ClassLoader cl)
public org.apache.commons.logging.Log getLog()
public void importBeans(java.lang.String resourcePattern)
resourcePattern - The resource patternpublic void xmlns(java.util.Map<java.lang.String,java.lang.String> definition)
definition - The definitionpublic org.springframework.context.ApplicationContext getParentCtx()
public RuntimeSpringConfiguration getSpringConfig()
public org.springframework.beans.factory.config.BeanDefinition getBeanDefinition(java.lang.String name)
name - The bean definitionpublic java.util.Map<java.lang.String,org.springframework.beans.factory.config.BeanDefinition> getBeanDefinitions()
public void setSpringConfig(RuntimeSpringConfiguration springConfig)
springConfig - The spring configpublic void loadBeans(java.lang.String resourcePattern)
throws java.io.IOException
resourcePattern - The resource patternjava.io.IOException - When the path cannot be matchedpublic void loadBeans(org.springframework.core.io.Resource resource)
resource - The resource to loadpublic void loadBeans(org.springframework.core.io.Resource[] resources)
resources - The resources to loadjava.io.IOException - Thrown if there is an error reading one of the passes resourcespublic void registerBeans(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)
public void registerBeans(RuntimeSpringConfiguration targetSpringConfig)
targetSpringConfig - The RuntimeSpringConfiguration objectpublic java.lang.Object invokeMethod(java.lang.String name,
java.lang.Object arg)
invokeMethod in interface groovy.lang.GroovyObjectinvokeMethod in class groovy.lang.GroovyObjectSupportpublic BeanBuilder beans(groovy.lang.Closure<?> c)
c - The block or closurepublic org.springframework.context.ApplicationContext createApplicationContext()
protected void finalizeDeferredProperties()
protected boolean addToDeferred(BeanConfiguration beanConfig, java.lang.String property, java.lang.Object newValue)
protected BeanConfiguration invokeBeanDefiningMethod(java.lang.String name, java.lang.Object[] args)
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 argumentsprotected java.util.List resolveConstructorArguments(java.lang.Object[] args,
int start,
int end)
protected java.lang.Object[] subarray(java.lang.Object[] args,
int i,
int j)
protected void filterGStringReferences(java.lang.Object[] constructorArgs)
protected BeanBuilder invokeBeanDefiningClosure(groovy.lang.Closure<?> callable)
callable - The closure argumentpublic void setProperty(java.lang.String name,
java.lang.Object value)
setProperty in interface groovy.lang.GroovyObjectsetProperty in class groovy.lang.GroovyObjectSupportpublic org.springframework.beans.factory.support.AbstractBeanDefinition bean(java.lang.Class<?> type)
type - The bean typepublic org.springframework.beans.factory.support.AbstractBeanDefinition bean(java.lang.Class type,
java.lang.Object... args)
type - The bean typeargs - The constructors arguments and closure configurerprotected void setPropertyOnBeanConfig(java.lang.String name,
java.lang.Object value)
protected java.lang.Object manageMapIfNecessary(java.lang.Object value)
value - The current mapprotected java.lang.Object manageListIfNecessary(java.lang.Object value)
value - The object that represents the listpublic java.lang.Object getProperty(java.lang.String name)
getProperty in interface groovy.lang.GroovyObjectgetProperty in class groovy.lang.GroovyObjectSupportprotected grails.spring.DynamicElementReader createDynamicElementReader(java.lang.String namespace,
boolean decorator)
public void setBinding(groovy.lang.Binding b)
b - The Binding instance