open class GroovyBeanDefinitionReader : AbstractBeanDefinitionReader, GroovyObject
A Groovy-based reader for Spring bean definitions: like a Groovy builder, but more of a DSL for Spring configuration.
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 org.springframework.beans.factory.support.DefaultListableBeanFactory or a org.springframework.context.support.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 #loadBeanDefinitions(Resource...) or #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 } } }
Author
Jeff Brown
Author
Graeme Rocher
Author
Juergen Hoeller
Author
Sam Brannen
Since
4.0
See Also
BeanDefinitionRegistryorg.springframework.beans.factory.support.DefaultListableBeanFactoryorg.springframework.context.support.GenericApplicationContextorg.springframework.context.support.GenericGroovyApplicationContext
GroovyBeanDefinitionReader(registry: BeanDefinitionRegistry)
Create a new GroovyBeanDefinitionReader(xmlBeanDefinitionReader: XmlBeanDefinitionReader)
Create a new The supplied |
open fun bean(type: Class<*>): GenericBeanDefinitionopen fun bean(type: Class<*>, vararg args: Any): AbstractBeanDefinition
Define an inner bean definition. |
|
open fun beans(closure: Closure<Any>): GroovyBeanDefinitionReader
Defines a set of beans for the given block or closure. |
|
open fun getBinding(): Binding
Return a specified binding for Groovy variables, if any. |
|
open fun getMetaClass(): MetaClass |
|
open fun getProperty(name: String): Any
This method overrides property retrieval in the scope of the
|
|
open fun importBeans(resourcePattern: String): Unit
Import Spring bean definitions from either XML or Groovy sources into the current bean builder instance. |
|
open fun invokeMethod(name: String, arg: Any): Any
This method overrides method invocation to create beans for each method name that takes a class argument. |
|
open fun loadBeanDefinitions(resource: Resource): Intopen fun loadBeanDefinitions(encodedResource: EncodedResource): Int
Load bean definitions from the specified Groovy script or XML file. Note that |
|
open fun setBinding(binding: Binding): Unit
Set the binding, i.e. the Groovy variables available in the scope of a |
|
open fun setMetaClass(metaClass: MetaClass): Unit |
|
open fun setProperty(name: String, value: Any): Unit
This method overrides property setting in the scope of the |
|
open fun xmlns(definition: MutableMap<String, String>): Unit
Define a Spring XML namespace definition to use. |
open fun getBeanClassLoader(): ClassLoader |
|
fun getBeanFactory(): BeanDefinitionRegistry |
|
open fun getBeanNameGenerator(): BeanNameGenerator |
|
open fun getEnvironment(): Environment |
|
fun getRegistry(): BeanDefinitionRegistry |
|
open fun getResourceLoader(): ResourceLoader |
|
open fun loadBeanDefinitions(vararg resources: Resource): Intopen fun loadBeanDefinitions(location: String): Intopen fun loadBeanDefinitions(vararg locations: String): Intopen fun loadBeanDefinitions(location: String, actualResources: MutableSet<Resource>): Int
Load bean definitions from the specified resource location. The location can also be a location pattern, provided that the ResourceLoader of this bean definition reader is a ResourcePatternResolver. |
|
open fun setBeanClassLoader(beanClassLoader: ClassLoader): Unit
Set the ClassLoader to use for bean classes. Default is |
|
open fun setBeanNameGenerator(beanNameGenerator: BeanNameGenerator): Unit
Set the BeanNameGenerator to use for anonymous beans (without explicit bean name specified). Default is a DefaultBeanNameGenerator. |
|
open fun setEnvironment(environment: Environment): Unit
Set the Environment to use when reading bean definitions. Most often used for evaluating profile information to determine which bean definitions should be read and which should be omitted. |
|
open fun setResourceLoader(resourceLoader: ResourceLoader): Unit
Set the ResourceLoader to use for resource locations. If specifying a ResourcePatternResolver, the bean definition reader will be capable of resolving resource patterns to Resource arrays. Default is PathMatchingResourcePatternResolver, also capable of resource pattern resolving through the ResourcePatternResolver interface. Setting this to |