public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader
This bean definition reader also understands XML bean definition files, allowing for seamless mixing and matching with Groovy bean definition files.
Typically applied to a
DefaultListableBeanFactory
or a GenericApplicationContext
,
but can be used against any BeanDefinitionRegistry
implementation.
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 in a Groovy script using
either the AbstractBeanDefinitionReader.loadBeanDefinitions(Resource...)
or
AbstractBeanDefinitionReader.loadBeanDefinitions(String...)
method, with a script looking similar to
the following.
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 } } }
BeanDefinitionRegistry
,
DefaultListableBeanFactory
,
GenericApplicationContext
,
GenericGroovyApplicationContext
Modifier and Type | Class and Description |
---|---|
private static class |
GroovyBeanDefinitionReader.DeferredProperty
This class is used to defer the adding of a property to a bean definition
until later.
|
private class |
GroovyBeanDefinitionReader.GroovyRuntimeBeanReference
A RuntimeBeanReference that takes care of adding new properties to runtime references.
|
Modifier and Type | Field and Description |
---|---|
private Binding |
binding |
private GroovyBeanDefinitionWrapper |
currentBeanDefinition |
private java.util.Map<java.lang.String,GroovyBeanDefinitionReader.DeferredProperty> |
deferredProperties |
private XmlBeanDefinitionReader |
groovyDslXmlBeanDefinitionReader
Groovy DSL
XmlBeanDefinitionReader for loading bean definitions
via the Groovy DSL, typically configured with XML validation disabled. |
private MetaClass |
metaClass |
private java.util.Map<java.lang.String,java.lang.String> |
namespaces |
private XmlBeanDefinitionReader |
standardXmlBeanDefinitionReader
Standard
XmlBeanDefinitionReader created with default
settings for loading bean definitions from XML files. |
logger
Constructor and Description |
---|
GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
Create a new
GroovyBeanDefinitionReader for the given
BeanDefinitionRegistry . |
GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
Create a new
GroovyBeanDefinitionReader based on the given
XmlBeanDefinitionReader , loading bean definitions into its
BeanDefinitionRegistry and delegating Groovy DSL loading to it. |
Modifier and Type | Method and Description |
---|---|
private boolean |
addDeferredProperty(java.lang.String property,
java.lang.Object newValue) |
protected void |
applyPropertyToBeanDefinition(java.lang.String name,
java.lang.Object value) |
GenericBeanDefinition |
bean(java.lang.Class<?> type)
Define an inner bean definition.
|
AbstractBeanDefinition |
bean(java.lang.Class<?> type,
java.lang.Object... args)
Define an inner bean definition.
|
GroovyBeanDefinitionReader |
beans(Closure closure)
Defines a set of beans for the given block or closure.
|
private GroovyDynamicElementReader |
createDynamicElementReader(java.lang.String namespace) |
private void |
finalizeDeferredProperties() |
Binding |
getBinding()
Return a specified binding for Groovy variables, if any.
|
MetaClass |
getMetaClass() |
java.lang.Object |
getProperty(java.lang.String name)
This method overrides property retrieval in the scope of the
GroovyBeanDefinitionReader to either:
Retrieve a variable from the bean builder's binding if it exists
Retrieve a RuntimeBeanReference for a specific bean if it exists
Otherwise just delegate to MetaClass.getProperty which will resolve
properties from the GroovyBeanDefinitionReader itself
|
void |
importBeans(java.lang.String resourcePattern)
Import Spring bean definitions from either XML or Groovy sources into the
current bean builder instance.
|
protected GroovyBeanDefinitionReader |
invokeBeanDefiningClosure(Closure callable)
When a method argument is only a closure it is a set of bean definitions.
|
private GroovyBeanDefinitionWrapper |
invokeBeanDefiningMethod(java.lang.String beanName,
java.lang.Object[] args)
This method is called when a bean definition node is called.
|
java.lang.Object |
invokeMethod(java.lang.String name,
java.lang.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 or XML file.
|
int |
loadBeanDefinitions(Resource resource)
Load bean definitions from the specified Groovy script or XML file.
|
private java.lang.Object |
manageListIfNecessary(java.util.List<?> list)
Checks whether there are any
RuntimeBeanReference s inside the List
and converts it to a ManagedList if necessary. |
private java.lang.Object |
manageMapIfNecessary(java.util.Map<?,?> map)
Checks whether there are any
RuntimeBeanReference s inside the Map
and converts it to a ManagedMap if necessary. |
protected java.util.List<java.lang.Object> |
resolveConstructorArguments(java.lang.Object[] args,
int start,
int end) |
void |
setBinding(Binding binding)
Set the binding, i.e.
|
void |
setMetaClass(MetaClass metaClass) |
void |
setProperty(java.lang.String name,
java.lang.Object value)
This method overrides property setting in the scope of the
GroovyBeanDefinitionReader
to set properties on the current bean definition. |
void |
xmlns(java.util.Map<java.lang.String,java.lang.String> definition)
Define a Spring XML namespace definition to use.
|
getBeanClassLoader, getBeanFactory, getBeanNameGenerator, getEnvironment, getRegistry, getResourceLoader, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, setBeanClassLoader, setBeanNameGenerator, setEnvironment, setResourceLoader
private final XmlBeanDefinitionReader standardXmlBeanDefinitionReader
XmlBeanDefinitionReader
created with default
settings for loading bean definitions from XML files.private final XmlBeanDefinitionReader groovyDslXmlBeanDefinitionReader
XmlBeanDefinitionReader
for loading bean definitions
via the Groovy DSL, typically configured with XML validation disabled.private final java.util.Map<java.lang.String,java.lang.String> namespaces
private final java.util.Map<java.lang.String,GroovyBeanDefinitionReader.DeferredProperty> deferredProperties
private MetaClass metaClass
private Binding binding
private GroovyBeanDefinitionWrapper currentBeanDefinition
public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
GroovyBeanDefinitionReader
for the given
BeanDefinitionRegistry
.registry
- the BeanDefinitionRegistry
to load bean definitions intopublic GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
GroovyBeanDefinitionReader
based on the given
XmlBeanDefinitionReader
, loading bean definitions into its
BeanDefinitionRegistry
and delegating Groovy DSL loading to it.
The supplied XmlBeanDefinitionReader
should typically
be pre-configured with XML validation disabled.
xmlBeanDefinitionReader
- the XmlBeanDefinitionReader
to
derive the registry from and to delegate Groovy DSL loading topublic void setMetaClass(MetaClass metaClass)
public MetaClass getMetaClass()
public void setBinding(Binding binding)
GroovyBeanDefinitionReader
closure.public Binding getBinding()
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException
Note that ".xml"
files will be parsed as XML content; all other kinds
of resources will be parsed as Groovy scripts.
resource
- the resource descriptor for the Groovy script or XML fileBeanDefinitionStoreException
- in case of loading or parsing errorspublic int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException
Note that ".xml"
files will be parsed as XML content; all other kinds
of resources will be parsed as Groovy scripts.
encodedResource
- the resource descriptor for the Groovy script or XML file,
allowing specification of an encoding to use for parsing the fileBeanDefinitionStoreException
- in case of loading or parsing errorspublic GroovyBeanDefinitionReader beans(Closure closure)
closure
- the block or closureGroovyBeanDefinitionReader
instancepublic GenericBeanDefinition bean(java.lang.Class<?> type)
type
- the bean typepublic AbstractBeanDefinition bean(java.lang.Class<?> type, java.lang.Object... args)
type
- the bean typeargs
- the constructors arguments and closure configurerpublic void xmlns(java.util.Map<java.lang.String,java.lang.String> definition)
definition
- the namespace definitionpublic void importBeans(java.lang.String resourcePattern) throws java.io.IOException
resourcePattern
- the resource patternjava.io.IOException
public java.lang.Object invokeMethod(java.lang.String name, java.lang.Object arg)
private boolean addDeferredProperty(java.lang.String property, java.lang.Object newValue)
private void finalizeDeferredProperties()
protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(Closure callable)
callable
- the closure argumentGroovyBeanDefinitionReader
instanceprivate GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(java.lang.String beanName, java.lang.Object[] args)
beanName
- 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 java.util.List<java.lang.Object> resolveConstructorArguments(java.lang.Object[] args, int start, int end)
private java.lang.Object manageMapIfNecessary(java.util.Map<?,?> map)
RuntimeBeanReference
s inside the Map
and converts it to a ManagedMap
if necessary.map
- the original Mapprivate java.lang.Object manageListIfNecessary(java.util.List<?> list)
RuntimeBeanReference
s inside the List
and converts it to a ManagedList
if necessary.list
- the original Listpublic void setProperty(java.lang.String name, java.lang.Object value)
GroovyBeanDefinitionReader
to set properties on the current bean definition.protected void applyPropertyToBeanDefinition(java.lang.String name, java.lang.Object value)
public java.lang.Object getProperty(java.lang.String name)
GroovyBeanDefinitionReader
to either:
GroovyBeanDefinitionReader
itself
private GroovyDynamicElementReader createDynamicElementReader(java.lang.String namespace)