Class GroovyBeanDefinitionReader
- All Implemented Interfaces:
- groovy.lang.GroovyObject,- BeanDefinitionReader,- EnvironmentCapable
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.
 
Example Syntax
 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
         }
     }
 }- Since:
- 4.0
- Author:
- Jeff Brown, Graeme Rocher, Juergen Hoeller, Sam Brannen
- See Also:
- 
Field SummaryFields inherited from class org.springframework.beans.factory.support.AbstractBeanDefinitionReaderlogger
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newGroovyBeanDefinitionReaderfor the givenBeanDefinitionRegistry.GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader) Create a newGroovyBeanDefinitionReaderbased on the givenXmlBeanDefinitionReader, loading bean definitions into itsBeanDefinitionRegistryand delegating Groovy DSL loading to it.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidapplyPropertyToBeanDefinition(String name, Object value) Define an inner bean definition.Define an inner bean definition.beans(groovy.lang.Closure<?> closure) Defines a set of beans for the given block or closure.@Nullable groovy.lang.BindingReturn a specified binding for Groovy variables, if any.groovy.lang.MetaClassgetProperty(String name) This method overrides property retrieval in the scope of theGroovyBeanDefinitionReader.voidimportBeans(String resourcePattern) Import Spring bean definitions from either XML or Groovy sources into the current bean builder instance.protected GroovyBeanDefinitionReaderinvokeBeanDefiningClosure(groovy.lang.Closure<?> callable) When a method argument is only a closure it is a set of bean definitions.invokeMethod(String name, Object arg) This method overrides method invocation to create beans for each method name that takes a class argument.intloadBeanDefinitions(Resource resource) Load bean definitions from the specified Groovy script or XML file.intloadBeanDefinitions(EncodedResource encodedResource) Load bean definitions from the specified Groovy script or XML file.resolveConstructorArguments(Object[] args, int start, int end) voidsetBinding(groovy.lang.Binding binding) Set the binding, i.e.voidsetMetaClass(groovy.lang.MetaClass metaClass) voidsetProperty(String name, Object value) This method overrides property setting in the scope of theGroovyBeanDefinitionReaderto set properties on the current bean definition.voidDefine a Spring XML namespace definition to use.Methods inherited from class org.springframework.beans.factory.support.AbstractBeanDefinitionReadergetBeanClassLoader, getBeanNameGenerator, getEnvironment, getRegistry, getResourceLoader, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, setBeanClassLoader, setBeanNameGenerator, setEnvironment, setResourceLoader
- 
Constructor Details- 
GroovyBeanDefinitionReaderCreate a newGroovyBeanDefinitionReaderfor the givenBeanDefinitionRegistry.- Parameters:
- registry- the- BeanDefinitionRegistryto load bean definitions into
 
- 
GroovyBeanDefinitionReaderCreate a newGroovyBeanDefinitionReaderbased on the givenXmlBeanDefinitionReader, loading bean definitions into itsBeanDefinitionRegistryand delegating Groovy DSL loading to it.The supplied XmlBeanDefinitionReadershould typically be pre-configured with XML validation disabled.- Parameters:
- xmlBeanDefinitionReader- the- XmlBeanDefinitionReaderto derive the registry from and to delegate Groovy DSL loading to
 
 
- 
- 
Method Details- 
setMetaClasspublic void setMetaClass(groovy.lang.MetaClass metaClass) - Specified by:
- setMetaClassin interface- groovy.lang.GroovyObject
 
- 
getMetaClasspublic groovy.lang.MetaClass getMetaClass()- Specified by:
- getMetaClassin interface- groovy.lang.GroovyObject
 
- 
setBindingpublic void setBinding(groovy.lang.Binding binding) Set the binding, i.e. the Groovy variables available in the scope of aGroovyBeanDefinitionReaderclosure.
- 
getBindingReturn a specified binding for Groovy variables, if any.
- 
loadBeanDefinitionsLoad bean definitions from the specified Groovy script or XML file.Note that ".xml"files will be parsed as XML content; all other kinds of resources will be parsed as Groovy scripts.- Specified by:
- loadBeanDefinitionsin interface- BeanDefinitionReader
- Parameters:
- resource- the resource descriptor for the Groovy script or XML file
- Returns:
- the number of bean definitions found
- Throws:
- BeanDefinitionStoreException- in case of loading or parsing errors
 
- 
loadBeanDefinitionsLoad bean definitions from the specified Groovy script or XML file.Note that ".xml"files will be parsed as XML content; all other kinds of resources will be parsed as Groovy scripts.- Parameters:
- encodedResource- the resource descriptor for the Groovy script or XML file, allowing specification of an encoding to use for parsing the file
- Returns:
- the number of bean definitions found
- Throws:
- BeanDefinitionStoreException- in case of loading or parsing errors
 
- 
beansDefines a set of beans for the given block or closure.- Parameters:
- closure- the block or closure
- Returns:
- this GroovyBeanDefinitionReaderinstance
 
- 
beanDefine an inner bean definition.- Parameters:
- type- the bean type
- Returns:
- the bean definition
 
- 
beanDefine an inner bean definition.- Parameters:
- type- the bean type
- args- the constructors arguments and closure configurer
- Returns:
- the bean definition
 
- 
xmlns
- 
importBeansImport Spring bean definitions from either XML or Groovy sources into the current bean builder instance.- Parameters:
- resourcePattern- the resource pattern
- Throws:
- IOException
 
- 
invokeMethod
- 
invokeBeanDefiningClosureWhen a method argument is only a closure it is a set of bean definitions.- Parameters:
- callable- the closure argument
- Returns:
- this GroovyBeanDefinitionReaderinstance
 
- 
resolveConstructorArguments
- 
setProperty
- 
applyPropertyToBeanDefinition
- 
getPropertyThis method overrides property retrieval in the scope of theGroovyBeanDefinitionReader. A property retrieval will 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 GroovyBeanDefinitionReaderitself
 - Specified by:
- getPropertyin interface- groovy.lang.GroovyObject
 
 
-