|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object groovy.lang.GroovyObjectSupport grails.spring.BeanBuilder
public class BeanBuilder
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
Constructor Summary | |
---|---|
BeanBuilder()
|
|
BeanBuilder(ApplicationContext parent)
|
|
BeanBuilder(ApplicationContext parent,
ClassLoader classLoader)
|
|
BeanBuilder(ApplicationContext parentCtx,
RuntimeSpringConfiguration springConfig,
ClassLoader classLoader)
|
|
BeanBuilder(ClassLoader classLoader)
|
Method Summary | |
---|---|
protected boolean |
addToDeferred(BeanConfiguration beanConfig,
String property,
Object newValue)
|
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)
|
protected void |
filterGStringReferences(Object[] constructorArgs)
|
protected void |
finalizeDeferredProperties()
|
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 |
void |
importBeans(String resourcePattern)
Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance |
protected void |
initializeBeanBuilderForClassLoader(ClassLoader classLoader)
|
protected void |
initializeSpringConfig()
|
protected BeanBuilder |
invokeBeanDefiningClosure(groovy.lang.Closure callable)
When an methods argument is only a closure it is a set of bean definitions |
protected BeanConfiguration |
invokeBeanDefiningMethod(String name,
Object[] args)
This method is called when a bean definition node is called |
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") |
protected Object |
manageListIfNecessary(Object value)
Checks whether there are any runtime refs inside the list and converts it to a ManagedList if necessary |
protected Object |
manageMapIfNecessary(Object value)
Checks whether there are any runtime refs inside a Map and converts it to a ManagedMap if necessary |
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. |
protected List |
resolveConstructorArguments(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(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 |
protected void |
setPropertyOnBeanConfig(String name,
Object value)
|
void |
setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver)
|
void |
setSpringConfig(RuntimeSpringConfiguration springConfig)
Sets the runtime Spring configuration instance to use. |
protected Object[] |
subarray(Object[] args,
int i,
int j)
|
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 |
---|
public BeanBuilder()
public BeanBuilder(ClassLoader classLoader)
public BeanBuilder(ApplicationContext parent)
public BeanBuilder(ApplicationContext parent, ClassLoader classLoader)
public BeanBuilder(ApplicationContext parentCtx, RuntimeSpringConfiguration springConfig, ClassLoader classLoader)
Method Detail |
---|
public void setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver)
protected void initializeSpringConfig()
public void setClassLoader(ClassLoader classLoader)
protected void initializeBeanBuilderForClassLoader(ClassLoader classLoader)
public void setNamespaceHandlerResolver(NamespaceHandlerResolver namespaceHandlerResolver)
protected RuntimeSpringConfiguration createRuntimeSpringConfiguration(ApplicationContext parent, ClassLoader classLoader)
public org.apache.commons.logging.Log getLog()
public void importBeans(String resourcePattern)
resourcePattern
- The resource patternpublic void xmlns(Map<String,String> definition)
definition
- The definitionpublic ApplicationContext getParentCtx()
public RuntimeSpringConfiguration getSpringConfig()
public BeanDefinition getBeanDefinition(String name)
name
- The bean definition
public Map<String,BeanDefinition> getBeanDefinitions()
public void setSpringConfig(RuntimeSpringConfiguration springConfig)
springConfig
- The spring configpublic void loadBeans(String resourcePattern) throws IOException
resourcePattern
- The resource pattern
IOException
- When the path cannot be matchedpublic void loadBeans(Resource resource) throws IOException
resource
- The resource to load
IOException
- When an error occurspublic void loadBeans(Resource[] resources) throws IOException
resources
- The resources to load
IOException
- Thrown if there is an error reading one of the passes resourcespublic void registerBeans(BeanDefinitionRegistry registry)
public void registerBeans(RuntimeSpringConfiguration targetSpringConfig)
targetSpringConfig
- The RuntimeSpringConfiguration objectpublic Object invokeMethod(String name, Object arg)
invokeMethod
in interface groovy.lang.GroovyObject
invokeMethod
in class groovy.lang.GroovyObjectSupport
public BeanBuilder beans(groovy.lang.Closure c)
c
- The block or closure
public ApplicationContext createApplicationContext()
protected void finalizeDeferredProperties()
protected boolean addToDeferred(BeanConfiguration beanConfig, String property, Object newValue)
protected BeanConfiguration invokeBeanDefiningMethod(String name, 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 arguments
protected List resolveConstructorArguments(Object[] args, int start, int end)
protected Object[] subarray(Object[] args, int i, int j)
protected void filterGStringReferences(Object[] constructorArgs)
protected BeanBuilder invokeBeanDefiningClosure(groovy.lang.Closure callable)
callable
- The closure argument
public void setProperty(String name, Object value)
setProperty
in interface groovy.lang.GroovyObject
setProperty
in class groovy.lang.GroovyObjectSupport
protected void setPropertyOnBeanConfig(String name, Object value)
protected Object manageMapIfNecessary(Object value)
value
- The current map
protected Object manageListIfNecessary(Object value)
value
- The object that represents the list
public Object getProperty(String name)
getProperty
in interface groovy.lang.GroovyObject
getProperty
in class groovy.lang.GroovyObjectSupport
protected DynamicElementReader createDynamicElementReader(String namespace, boolean decorator)
public void setBinding(groovy.lang.Binding b)
b
- The Binding instance
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |