类 GroovyBeanDefinitionReader
- java.lang.Object
- org.springframework.beans.factory.support.AbstractBeanDefinitionReader
- org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
- 所有已实现的接口:
groovy.lang.GroovyObject
,BeanDefinitionReader
,EnvironmentCapable
public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader implements groovy.lang.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
DefaultListableBeanFactory
or aGenericApplicationContext
, but can be used against anyBeanDefinitionRegistry
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...)
orAbstractBeanDefinitionReader.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 } } }
- 从以下版本开始:
- 4.0
- 作者:
- Jeff Brown, Graeme Rocher, Juergen Hoeller, Sam Brannen
- 另请参阅:
BeanDefinitionRegistry
,DefaultListableBeanFactory
,GenericApplicationContext
,GenericGroovyApplicationContext
字段概要
从类继承的字段 org.springframework.beans.factory.support.AbstractBeanDefinitionReader
logger
构造器概要
构造器 构造器 说明 GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
Create a newGroovyBeanDefinitionReader
for the givenBeanDefinitionRegistry
.GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
Create a newGroovyBeanDefinitionReader
based on the givenXmlBeanDefinitionReader
, loading bean definitions into itsBeanDefinitionRegistry
and delegating Groovy DSL loading to it.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected void
applyPropertyToBeanDefinition(String name, Object value)
GenericBeanDefinition
bean(Class<?> type)
Define an inner bean definition.AbstractBeanDefinition
bean(Class<?> type, 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()
Object
getProperty(String name)
This method overrides property retrieval in the scope of theGroovyBeanDefinitionReader
.void
importBeans(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.Object
invokeMethod(String name, Object arg)
This method overrides method invocation to create beans for each method name that takes a class argument.int
loadBeanDefinitions(Resource resource)
Load bean definitions from the specified Groovy script or XML file.int
loadBeanDefinitions(EncodedResource encodedResource)
Load bean definitions from the specified Groovy script or XML file.protected List<Object>
resolveConstructorArguments(Object[] args, int start, int end)
void
setBinding(groovy.lang.Binding binding)
Set the binding, i.e. the Groovy variables available in the scope of aGroovyBeanDefinitionReader
closure.void
setMetaClass(groovy.lang.MetaClass metaClass)
void
setProperty(String name, Object value)
This method overrides property setting in the scope of theGroovyBeanDefinitionReader
to set properties on the current bean definition.void
xmlns(Map<String,String> definition)
Define a Spring XML namespace definition to use.从类继承的方法 org.springframework.beans.factory.support.AbstractBeanDefinitionReader
getBeanClassLoader, getBeanFactory, getBeanNameGenerator, getEnvironment, getRegistry, getResourceLoader, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, loadBeanDefinitions, setBeanClassLoader, setBeanNameGenerator, setEnvironment, setResourceLoader
构造器详细资料
GroovyBeanDefinitionReader
public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
Create a newGroovyBeanDefinitionReader
for the givenBeanDefinitionRegistry
.- 参数:
registry
- theBeanDefinitionRegistry
to load bean definitions into
GroovyBeanDefinitionReader
public GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
Create a newGroovyBeanDefinitionReader
based on the givenXmlBeanDefinitionReader
, loading bean definitions into itsBeanDefinitionRegistry
and delegating Groovy DSL loading to it.The supplied
XmlBeanDefinitionReader
should typically be pre-configured with XML validation disabled.- 参数:
xmlBeanDefinitionReader
- theXmlBeanDefinitionReader
to derive the registry from and to delegate Groovy DSL loading to
方法详细资料
setMetaClass
public void setMetaClass(groovy.lang.MetaClass metaClass)
- 指定者:
setMetaClass
在接口中groovy.lang.GroovyObject
getMetaClass
public groovy.lang.MetaClass getMetaClass()
- 指定者:
getMetaClass
在接口中groovy.lang.GroovyObject
setBinding
public void setBinding(groovy.lang.Binding binding)
Set the binding, i.e. the Groovy variables available in the scope of aGroovyBeanDefinitionReader
closure.
getBinding
public groovy.lang.Binding getBinding()
Return a specified binding for Groovy variables, if any.
loadBeanDefinitions
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException
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.- 指定者:
loadBeanDefinitions
在接口中BeanDefinitionReader
- 参数:
resource
- the resource descriptor for the Groovy script or XML file- 返回:
- the number of bean definitions found
- 抛出:
BeanDefinitionStoreException
- in case of loading or parsing errors
loadBeanDefinitions
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException
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.- 参数:
encodedResource
- the resource descriptor for the Groovy script or XML file, allowing specification of an encoding to use for parsing the file- 返回:
- the number of bean definitions found
- 抛出:
BeanDefinitionStoreException
- in case of loading or parsing errors
beans
public GroovyBeanDefinitionReader beans(groovy.lang.Closure closure)
Defines a set of beans for the given block or closure.- 参数:
closure
- the block or closure- 返回:
- this
GroovyBeanDefinitionReader
instance
bean
public GenericBeanDefinition bean(Class<?> type)
Define an inner bean definition.- 参数:
type
- the bean type- 返回:
- the bean definition
bean
public AbstractBeanDefinition bean(Class<?> type, Object... args)
Define an inner bean definition.- 参数:
type
- the bean typeargs
- the constructors arguments and closure configurer- 返回:
- the bean definition
xmlns
public void xmlns(Map<String,String> definition)
Define a Spring XML namespace definition to use.- 参数:
definition
- the namespace definition
importBeans
public void importBeans(String resourcePattern) throws IOException
Import Spring bean definitions from either XML or Groovy sources into the current bean builder instance.- 参数:
resourcePattern
- the resource pattern- 抛出:
IOException
invokeMethod
public Object invokeMethod(String name, Object arg)
This method overrides method invocation to create beans for each method name that takes a class argument.- 指定者:
invokeMethod
在接口中groovy.lang.GroovyObject
invokeBeanDefiningClosure
protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(groovy.lang.Closure callable)
When a method argument is only a closure it is a set of bean definitions.- 参数:
callable
- the closure argument- 返回:
- this
GroovyBeanDefinitionReader
instance
resolveConstructorArguments
protected List<Object> resolveConstructorArguments(Object[] args, int start, int end)
setProperty
public void setProperty(String name, Object value)
This method overrides property setting in the scope of theGroovyBeanDefinitionReader
to set properties on the current bean definition.- 指定者:
setProperty
在接口中groovy.lang.GroovyObject
applyPropertyToBeanDefinition
protected void applyPropertyToBeanDefinition(String name, Object value)
getProperty
public Object getProperty(String name)
This 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
GroovyBeanDefinitionReader
itself
- 指定者:
getProperty
在接口中groovy.lang.GroovyObject