spring-framework / org.springframework.beans.factory.groovy / GroovyBeanDefinitionReader

GroovyBeanDefinitionReader

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

Constructors

<init>

GroovyBeanDefinitionReader(registry: BeanDefinitionRegistry)

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.

The supplied XmlBeanDefinitionReader should typically be pre-configured with XML validation disabled.

Functions

bean

open fun bean(type: Class<*>): GenericBeanDefinition
open fun bean(type: Class<*>, vararg args: Any): AbstractBeanDefinition

Define an inner bean definition.

beans

open fun beans(closure: Closure<Any>): GroovyBeanDefinitionReader

Defines a set of beans for the given block or closure.

getBinding

open fun getBinding(): Binding

Return a specified binding for Groovy variables, if any.

getMetaClass

open fun getMetaClass(): MetaClass

getProperty

open fun getProperty(name: String): Any

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

importBeans

open fun importBeans(resourcePattern: String): Unit

Import Spring bean definitions from either XML or Groovy sources into the current bean builder instance.

invokeMethod

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.

loadBeanDefinitions

open fun loadBeanDefinitions(resource: Resource): Int
open fun loadBeanDefinitions(encodedResource: EncodedResource): Int

Load 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.

setBinding

open fun setBinding(binding: Binding): Unit

Set the binding, i.e. the Groovy variables available in the scope of a GroovyBeanDefinitionReader closure.

setMetaClass

open fun setMetaClass(metaClass: MetaClass): Unit

setProperty

open fun setProperty(name: String, value: Any): Unit

This method overrides property setting in the scope of the GroovyBeanDefinitionReader to set properties on the current bean definition.

xmlns

open fun xmlns(definition: MutableMap<String, String>): Unit

Define a Spring XML namespace definition to use.

Inherited Functions

getBeanClassLoader

open fun getBeanClassLoader(): ClassLoader

getBeanFactory

fun getBeanFactory(): BeanDefinitionRegistry

getBeanNameGenerator

open fun getBeanNameGenerator(): BeanNameGenerator

getEnvironment

open fun getEnvironment(): Environment

getRegistry

fun getRegistry(): BeanDefinitionRegistry

getResourceLoader

open fun getResourceLoader(): ResourceLoader

loadBeanDefinitions

open fun loadBeanDefinitions(vararg resources: Resource): Int
open fun loadBeanDefinitions(location: String): Int
open 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.

setBeanClassLoader

open fun setBeanClassLoader(beanClassLoader: ClassLoader): Unit

Set the ClassLoader to use for bean classes.

Default is null, which suggests to not load bean classes eagerly but rather to just register bean definitions with class names, with the corresponding Classes to be resolved later (or never).

setBeanNameGenerator

open fun setBeanNameGenerator(beanNameGenerator: BeanNameGenerator): Unit

Set the BeanNameGenerator to use for anonymous beans (without explicit bean name specified).

Default is a DefaultBeanNameGenerator.

setEnvironment

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.

setResourceLoader

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 null suggests that absolute resource loading is not available for this bean definition reader.