public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader implements groovy.lang.GroovyObject
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 org.springframework.context.support.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,
org.springframework.context.support.GenericApplicationContext,
org.springframework.context.support.GenericGroovyApplicationContextlogger| 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 |
|---|---|
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(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() |
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(groovy.lang.Closure callable)
When a method argument is only a closure it is a set of bean definitions.
|
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(org.springframework.core.io.support.EncodedResource encodedResource)
Load bean definitions from the specified Groovy script or XML file.
|
int |
loadBeanDefinitions(org.springframework.core.io.Resource resource)
Load bean definitions from the specified Groovy script or XML file.
|
protected java.util.List<java.lang.Object> |
resolveConstructorArguments(java.lang.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(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, setResourceLoaderpublic 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(groovy.lang.MetaClass metaClass)
setMetaClass in interface groovy.lang.GroovyObjectpublic groovy.lang.MetaClass getMetaClass()
getMetaClass in interface groovy.lang.GroovyObjectpublic void setBinding(groovy.lang.Binding binding)
GroovyBeanDefinitionReader closure.public groovy.lang.Binding getBinding()
public int loadBeanDefinitions(org.springframework.core.io.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.
loadBeanDefinitions in interface BeanDefinitionReaderresource - the resource descriptor for the Groovy script or XML fileBeanDefinitionStoreException - in case of loading or parsing errorspublic int loadBeanDefinitions(org.springframework.core.io.support.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(groovy.lang.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.IOExceptionpublic java.lang.Object invokeMethod(java.lang.String name,
java.lang.Object arg)
invokeMethod in interface groovy.lang.GroovyObjectprotected GroovyBeanDefinitionReader invokeBeanDefiningClosure(groovy.lang.Closure callable)
callable - the closure argumentGroovyBeanDefinitionReader instanceprotected java.util.List<java.lang.Object> resolveConstructorArguments(java.lang.Object[] args,
int start,
int end)
public void setProperty(java.lang.String name,
java.lang.Object value)
GroovyBeanDefinitionReader
to set properties on the current bean definition.setProperty in interface groovy.lang.GroovyObjectprotected void applyPropertyToBeanDefinition(java.lang.String name,
java.lang.Object value)
public java.lang.Object getProperty(java.lang.String name)
GroovyBeanDefinitionReader to either:
GroovyBeanDefinitionReader itself
getProperty in interface groovy.lang.GroovyObject