Top Description Fields Constructors Methods
org.springframework.context.support

public Class GenericGroovyApplicationContext

extends GenericApplicationContext
implements GroovyObject
Class Inheritance
All Implemented Interfaces
groovy.lang.GroovyObject
Imports
groovy.lang.GroovyObject, .GroovySystem, .MetaClass, org.springframework.beans.BeanWrapper, .BeanWrapperImpl, org.springframework.beans.factory.NoSuchBeanDefinitionException, org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader, org.springframework.core.env.ConfigurableEnvironment, org.springframework.core.io.ClassPathResource, .Resource, org.springframework.lang.Nullable

An org.springframework.context.ApplicationContext implementation that extends GenericApplicationContext and implements GroovyObject such that beans can be retrieved with the dot de-reference syntax instead of using getBean.

Consider this as the equivalent of GenericXmlApplicationContext for Groovy bean definitions, or even an upgrade thereof since it seamlessly understands XML bean definition files as well. The main difference is that, within a Groovy script, the context can be used with an inline bean definition closure as follows:

import org.hibernate.SessionFactory
import org.apache.commons.dbcp.BasicDataSource

def context = new GenericGroovyApplicationContext()
context.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
        }
    }
}
context.refresh()

Alternatively, load a Groovy bean definition script like the following from an external resource (e.g. an "applicationContext.groovy" file):

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

With the following Java code creating the GenericGroovyApplicationContext (potentially using Ant-style '*'/'**' location patterns):

GenericGroovyApplicationContext context = new GenericGroovyApplicationContext();
context.load("org/myapp/applicationContext.groovy");
context.refresh();

Or even more concise, provided that no extra configuration is needed:

ApplicationContext context = new GenericGroovyApplicationContext("org/myapp/applicationContext.groovy");

This application context also understands XML bean definition files, allowing for seamless mixing and matching with Groovy bean definition files. ".xml" files will be parsed as XML content; all other kinds of resources will be parsed as Groovy scripts.

Authors
Juergen Hoeller, Jeff Brown
Since
4.0
See Also
org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader

Field Summary

Modifier and TypeField and Description
private final BeanWrapper
private MetaClass
private final GroovyBeanDefinitionReader

Constructor Summary

AccessConstructor and Description
public
GenericGroovyApplicationContext()

Create a new GenericGroovyApplicationContext that needs to be loaded and then manually refreshed.

public
GenericGroovyApplicationContext(Resource...
the resources to load from
resources
)

Create a new GenericGroovyApplicationContext, loading bean definitions from the given resources and automatically refreshing the context.

public
GenericGroovyApplicationContext(String...
the resources to load from
resourceLocations
)

Create a new GenericGroovyApplicationContext, loading bean definitions from the given resource locations and automatically refreshing the context.

public
GenericGroovyApplicationContext(Class<?>
class whose package will be used as a prefix when loading each specified resource name
relativeClass
,
String...
relatively-qualified names of resources to load
resourceNames
)

Create a new GenericGroovyApplicationContext, loading bean definitions from the given resource locations and automatically refreshing the context.

Method Summary

Modifier and TypeMethod and Description
public MetaClass
getMetaClass()

Implements groovy.lang.GroovyObject.getMetaClass.

Returns the metaclass for a given class.

public Object
getProperty(String
the name of the property of interest
property
)

Overrides default groovy.lang.GroovyObject.getProperty.

Retrieves a property value.

public final GroovyBeanDefinitionReader
getReader()

Exposes the underlying GroovyBeanDefinitionReader for convenient access to the loadBeanDefinition methods on it as well as the ability to specify an inline Groovy bean definition closure.

public Object
invokeMethod(String
the name of the method to call
name
,
Object
the arguments to use for the method call
args
)

Overrides default groovy.lang.GroovyObject.invokeMethod.

Invokes the given method.

public void
load(Resource...
one or more resources to load from
resources
)

Load bean definitions from the given Groovy scripts or XML files.

public void
load(String...
one or more resource locations to load from
resourceLocations
)

Load bean definitions from the given Groovy scripts or XML files.

public void
load(Class<?>
class whose package will be used as a prefix when loading each specified resource name
relativeClass
,
String...
relatively-qualified names of resources to load
resourceNames
)

Load bean definitions from the given Groovy scripts or XML files.

public void
public void
setMetaClass(MetaClass
the new metaclass
metaClass
)

Implements groovy.lang.GroovyObject.setMetaClass.

Allows the MetaClass to be replaced with a derived implementation.

public void
setProperty(String
the name of the property of interest
property
,
Object
the new value for the property
newValue
)

Overrides default groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.

Inherited from org.springframework.context.support.GenericApplicationContext:
cancelRefreshcloseBeanFactorygetAutowireCapableBeanFactorygetBeanDefinitiongetBeanFactorygetClassLoadergetDefaultListableBeanFactorygetResourcegetResourcesisAliasisBeanDefinitionOverridableisBeanNameInUserefreshBeanFactoryrefreshForAotProcessingregisterAliasregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanDefinitionremoveAliasremoveBeanDefinitionsetAllowBeanDefinitionOverridingsetAllowCircularReferencessetApplicationStartupsetClassLoadersetParentsetResourceLoader

Field Detail

contextWrapperback to summary
private final BeanWrapper contextWrapper
metaClassback to summary
private MetaClass metaClass
readerback to summary
private final GroovyBeanDefinitionReader reader

Constructor Detail

GenericGroovyApplicationContextback to summary
public GenericGroovyApplicationContext()

Create a new GenericGroovyApplicationContext that needs to be loaded and then manually refreshed.

GenericGroovyApplicationContextback to summary
public GenericGroovyApplicationContext(Resource... resources)

Create a new GenericGroovyApplicationContext, loading bean definitions from the given resources and automatically refreshing the context.

Parameters
resources:Resource[]

the resources to load from

GenericGroovyApplicationContextback to summary
public GenericGroovyApplicationContext(String... resourceLocations)

Create a new GenericGroovyApplicationContext, loading bean definitions from the given resource locations and automatically refreshing the context.

Parameters
resourceLocations:String[]

the resources to load from

GenericGroovyApplicationContextback to summary
public GenericGroovyApplicationContext(Class<?> relativeClass, String... resourceNames)

Create a new GenericGroovyApplicationContext, loading bean definitions from the given resource locations and automatically refreshing the context.

Parameters
relativeClass:Class<?>

class whose package will be used as a prefix when loading each specified resource name

resourceNames:String[]

relatively-qualified names of resources to load

Method Detail

getMetaClassback to summary
public MetaClass getMetaClass()

Implements groovy.lang.GroovyObject.getMetaClass.

Doc from groovy.lang.GroovyObject.getMetaClass.

Returns the metaclass for a given class.

Returns:MetaClass

the metaClass of this instance

Annotations
@Override
getPropertyback to summary
public Object getProperty(String property)

Overrides default groovy.lang.GroovyObject.getProperty.

Doc from groovy.lang.GroovyObject.getProperty.

Retrieves a property value.

Parameters
property:String

the name of the property of interest

Returns:Object

the given property

Annotations
@Override
@Nullable
getReaderback to summary
public final GroovyBeanDefinitionReader getReader()

Exposes the underlying GroovyBeanDefinitionReader for convenient access to the loadBeanDefinition methods on it as well as the ability to specify an inline Groovy bean definition closure.

See Also
GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...), GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
invokeMethodback to summary
public Object invokeMethod(String name, Object args)

Overrides default groovy.lang.GroovyObject.invokeMethod.

Doc from groovy.lang.GroovyObject.invokeMethod.

Invokes the given method.

Parameters
name:String

the name of the method to call

args:Object

the arguments to use for the method call

Returns:Object

the result of invoking the method

Annotations
@Override
loadback to summary
public void load(Resource... resources)

Load bean definitions from the given Groovy scripts or XML files.

Note that ".xml" files will be parsed as XML content; all other kinds of resources will be parsed as Groovy scripts.

Parameters
resources:Resource[]

one or more resources to load from

loadback to summary
public void load(String... resourceLocations)

Load bean definitions from the given Groovy scripts or XML files.

Note that ".xml" files will be parsed as XML content; all other kinds of resources will be parsed as Groovy scripts.

Parameters
resourceLocations:String[]

one or more resource locations to load from

loadback to summary
public void load(Class<?> relativeClass, String... resourceNames)

Load bean definitions from the given Groovy scripts or XML files.

Note that ".xml" files will be parsed as XML content; all other kinds of resources will be parsed as Groovy scripts.

Parameters
relativeClass:Class<?>

class whose package will be used as a prefix when loading each specified resource name

resourceNames:String[]

relatively-qualified names of resources to load

setEnvironmentback to summary
public void setEnvironment(ConfigurableEnvironment environment)

Overrides org.springframework.context.support.AbstractApplicationContext.setEnvironment.

Implements org.springframework.context.ConfigurableApplicationContext.setEnvironment.

Delegates the given environment to underlying GroovyBeanDefinitionReader. Should be called before any call to #load.

Parameters
environment:ConfigurableEnvironment

Doc from org.springframework.context.ConfigurableApplicationContext.setEnvironment.

the new environment

Annotations
@Override
setMetaClassback to summary
public void setMetaClass(MetaClass metaClass)

Implements groovy.lang.GroovyObject.setMetaClass.

Doc from groovy.lang.GroovyObject.setMetaClass.

Allows the MetaClass to be replaced with a derived implementation.

Parameters
metaClass:MetaClass

the new metaclass

Annotations
@Override
setPropertyback to summary
public void setProperty(String property, Object newValue)

Overrides default groovy.lang.GroovyObject.setProperty.

Doc from groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.

Parameters
property:String

the name of the property of interest

newValue:Object

the new value for the property

Annotations
@Override