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

Package org.springframework.beans.factory.groovy

Types

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 } } }