public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader implements groovy.lang.GroovyObject
import org.hibernate.SessionFactory import org.apache.commons.dbcp.BasicDataSource def reader = new GroovyBeanDefinitionReader(myApplicationContext) reader.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 references } myService(MyService) { nestedBean = { AnotherBean bean -> // <-- setProperty with closure for nested bean dataSource = dataSource } } }
You can also load resources containing beans defined as a Groovy script using
either the AbstractBeanDefinitionReader.loadBeanDefinitions(org.springframework.core.io.Resource...)
or AbstractBeanDefinitionReader.loadBeanDefinitions(String...)
method, with a script looking as follows:
import org.hibernate.SessionFactory import org.apache.commons.dbcp.BasicDataSource beans { dataSource(BasicDataSource) { driverClassName = "org.hsqldb.jdbcDriver" url = "jdbc:hsqldb:mem:grailsDB" username = "sa" password = "" settings = [mynew:"setting"] } sessionFactory(SessionFactory) { dataSource = dataSource } myService(MyService) { nestedBean = { AnotherBean bean -> dataSource = dataSource } } }
Typically applied to a
DefaultListableBeanFactory
or a GenericApplicationContext
,
but can be used against any BeanDefinitionRegistry
implementation.
BeanDefinitionRegistry
,
DefaultListableBeanFactory
,
GenericApplicationContext
,
GenericGroovyApplicationContext
logger
Constructor and Description |
---|
GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
Create new GroovyBeanDefinitionReader for the given bean factory.
|
GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
Create new GroovyBeanDefinitionReader based on the given XmlBeanDefinitionReader,
using the same registry and delegating XML loading to it.
|
Modifier and Type | Method and Description |
---|---|
protected void |
applyPropertyToBeanDefinition(String name,
Object value) |
GenericBeanDefinition |
bean(Class<?> type)
Defines an inner bean definition.
|
AbstractBeanDefinition |
bean(Class<?> type,
Object... args)
Defines an inner bean definition
|
GroovyBeanDefinitionReader |
beans(groovy.lang.Closure closure)
Defines a set of beans for the given block or closure.
|
groovy.lang.Binding |
getBinding()
Return a specified binding for Groovy variables, if any.
|
groovy.lang.MetaClass |
getMetaClass() |
Object |
getProperty(String name)
This method overrides property retrieval in the scope of the GroovyBeanDefinitionReader 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 MetaClass.getProperty which will resolve properties from the GroovyBeanDefinitionReader itself
|
void |
importBeans(String resourcePattern)
Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance.
|
protected GroovyBeanDefinitionReader |
invokeBeanDefiningClosure(groovy.lang.Closure callable)
When an methods argument is only a closure it is a set of bean definitions.
|
Object |
invokeMethod(String name,
Object arg)
This method overrides method invocation to create beans for each method name that
takes a class argument
|
int |
loadBeanDefinitions(EncodedResource encodedResource)
Load bean definitions from the specified Groovy script.
|
int |
loadBeanDefinitions(Resource resource)
Load bean definitions from the specified Groovy script.
|
protected List<Object> |
resolveConstructorArguments(Object[] args,
int start,
int end) |
void |
setBinding(groovy.lang.Binding binding)
Set the binding, i.e.
|
void |
setMetaClass(groovy.lang.MetaClass metaClass) |
void |
setProperty(String name,
Object value)
This method overrides property setting in the scope of the GroovyBeanDefinitionReader
to set properties on the current bean definition.
|
void |
xmlns(Map<String,String> definition)
Defines an Spring namespace definition to use.
|
getBeanClassLoader, getBeanFactory, getBeanNameGenerator, getEnvironment, getRegistry, getResourceLoader, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, setBeanClassLoader, setBeanNameGenerator, setEnvironment, setResourceLoader
public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
registry
- the BeanFactory to load bean definitions into,
in the form of a BeanDefinitionRegistrypublic GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
xmlBeanDefinitionReader
- the XmlBeanDefinitionReader to derive the registry
from and to delegate XML loading topublic void setMetaClass(groovy.lang.MetaClass metaClass)
setMetaClass
in interface groovy.lang.GroovyObject
public groovy.lang.MetaClass getMetaClass()
getMetaClass
in interface groovy.lang.GroovyObject
public void setBinding(groovy.lang.Binding binding)
public groovy.lang.Binding getBinding()
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException
loadBeanDefinitions
in interface BeanDefinitionReader
resource
- the resource descriptor for the Groovy scriptBeanDefinitionStoreException
- in case of loading or parsing errorspublic int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException
encodedResource
- the resource descriptor for the Groovy script,
allowing to specify an encoding to use for parsing the fileBeanDefinitionStoreException
- in case of loading or parsing errorspublic GroovyBeanDefinitionReader beans(groovy.lang.Closure closure)
closure
- the block or closurepublic GenericBeanDefinition bean(Class<?> type)
type
- the bean typepublic AbstractBeanDefinition bean(Class<?> type, Object... args)
type
- The bean typeargs
- The constructors arguments and closure configurerpublic void xmlns(Map<String,String> definition)
definition
- the namespace definitionpublic void importBeans(String resourcePattern) throws IOException
resourcePattern
- the resource patternIOException
public Object invokeMethod(String name, Object arg)
invokeMethod
in interface groovy.lang.GroovyObject
protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(groovy.lang.Closure callable)
callable
- the closure argumentprotected List<Object> resolveConstructorArguments(Object[] args, int start, int end)
public void setProperty(String name, Object value)
setProperty
in interface groovy.lang.GroovyObject
protected void applyPropertyToBeanDefinition(String name, Object value)
public Object getProperty(String name)
getProperty
in interface groovy.lang.GroovyObject