Groovy-based bean definitions

Formerly known as the Grails BeanBuilder, now in Spring proper. Based on https://github.com/spring-projects/spring-framework/pull/355 but heavily refactored and restructured.

Issue: SPR-7123
This commit is contained in:
Juergen Hoeller
2013-10-17 19:24:08 +02:00
parent bb8cf277fb
commit 8d6406bbae
19 changed files with 2689 additions and 39 deletions

View File

@@ -0,0 +1,239 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.support;
import groovy.lang.GroovyObject;
import groovy.lang.GroovySystem;
import groovy.lang.MetaClass;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* An {@link org.springframework.context.ApplicationContext} implementation that extends
* {@link GenericApplicationContext} and implements {@link GroovyObject} such that beans
* can be retrieved with the dot de-reference syntax instead of using {@link #getBean}.
*
* <p>Consider this as the equivalent of {@link GenericXmlApplicationContext} for
* Groovy bean definitions. The main difference is that, within a Groovy script,
* the context can be used with an inline bean definition closure like as follows:
*
* <pre>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()</pre>
*
* <p>Alternatively, load a Groovy bean definition script like the following
* from an external resource (e.g. an "applicationContext.groovy" file):
*
* <pre>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
* }
* }
* }</pre>
*
* <p>With the following Java code creating the {@code GenericGroovyApplicationContext}
* (potentially using Ant-style '*'/'**' location patterns):
*
* <pre>GenericGroovyApplicationContext context = new GenericGroovyApplicationContext();
* context.load("org/myapp/applicationContext.groovy");
* context.refresh();</pre>
*
* <p>Or even more concise, provided that no extra configuration is needed:
*
* <pre>ApplicationContext context = new GenericGroovyApplicationContext("org/myapp/applicationContext.groovy");</pre>
*
* @author Juergen Hoeller
* @author Jeff Brown
* @since 4.0
* @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
*/
public class GenericGroovyApplicationContext extends GenericApplicationContext implements GroovyObject {
private final GroovyBeanDefinitionReader reader = new GroovyBeanDefinitionReader(this);
private final BeanWrapper contextWrapper = new BeanWrapperImpl(this);
private MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(getClass());
/**
* Create a new GenericGroovyApplicationContext that needs to be
* {@link #load loaded} and then manually {@link #refresh refreshed}.
*/
public GenericGroovyApplicationContext() {
}
/**
* Create a new GenericGroovyApplicationContext, loading bean definitions
* from the given resources and automatically refreshing the context.
* @param resources the resources to load from
*/
public GenericGroovyApplicationContext(Resource... resources) {
load(resources);
refresh();
}
/**
* Create a new GenericGroovyApplicationContext, loading bean definitions
* from the given resource locations and automatically refreshing the context.
* @param resourceLocations the resources to load from
*/
public GenericGroovyApplicationContext(String... resourceLocations) {
load(resourceLocations);
refresh();
}
/**
* Create a new GenericGroovyApplicationContext, loading bean definitions
* from the given resource locations and automatically refreshing the context.
* @param relativeClass class whose package will be used as a prefix when
* loading each specified resource name
* @param resourceNames relatively-qualified names of resources to load
*/
public GenericGroovyApplicationContext(Class<?> relativeClass, String... resourceNames) {
load(relativeClass, resourceNames);
refresh();
}
/**
* Exposes the underlying {@link GroovyBeanDefinitionReader} for convenient access
* to the {@code loadBeanDefinition} methods on it as well as the ability
* to specify an inline Groovy bean definition closure.
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...)
* @see GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
*/
public final GroovyBeanDefinitionReader getReader() {
return this.reader;
}
/**
* Delegates the given environment to underlying {@link GroovyBeanDefinitionReader}.
* Should be called before any call to {@code #load}.
*/
@Override
public void setEnvironment(ConfigurableEnvironment environment) {
super.setEnvironment(environment);
this.reader.setEnvironment(getEnvironment());
}
/**
* Load bean definitions from the given Groovy scripts.
* @param resources one or more resources to load from
*/
public void load(Resource... resources) {
this.reader.loadBeanDefinitions(resources);
}
/**
* Load bean definitions from the given Groovy scripts.
* @param resourceLocations one or more resource locations to load from
*/
public void load(String... resourceLocations) {
this.reader.loadBeanDefinitions(resourceLocations);
}
/**
* Load bean definitions from the given Groovy scripts.
* @param relativeClass class whose package will be used as a prefix when
* loading each specified resource name
* @param resourceNames relatively-qualified names of resources to load
*/
public void load(Class<?> relativeClass, String... resourceNames) {
Resource[] resources = new Resource[resourceNames.length];
for (int i = 0; i < resourceNames.length; i++) {
resources[i] = new ClassPathResource(resourceNames[i], relativeClass);
}
this.load(resources);
}
// IMPLEMENTATION OF THE GROOVYOBJECT INTERFACE
public void setMetaClass(MetaClass metaClass) {
this.metaClass = metaClass;
}
public MetaClass getMetaClass() {
return this.metaClass;
}
public Object invokeMethod(String name, Object args) {
return this.metaClass.invokeMethod(this, name, args);
}
public void setProperty(String property, Object newValue) {
if (newValue instanceof BeanDefinition) {
registerBeanDefinition(property, (BeanDefinition) newValue);
}
else {
this.metaClass.setProperty(this, property, newValue);
}
}
public Object getProperty(String property) {
if (containsBean(property)) {
return getBean(property);
}
else if (this.contextWrapper.isReadableProperty(property)) {
return this.contextWrapper.getPropertyValue(property);
}
throw new NoSuchBeanDefinitionException(property);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,10 +45,9 @@ public class GenericXmlApplicationContext extends GenericApplicationContext {
/**
* Create a new GenericXmlApplicationContext that needs to be
* {@linkplain #load loaded} and then manually {@link #refresh refreshed}.
* {@link #load loaded} and then manually {@link #refresh refreshed}.
*/
public GenericXmlApplicationContext() {
reader.setEnvironment(this.getEnvironment());
}
/**
@@ -83,6 +82,15 @@ public class GenericXmlApplicationContext extends GenericApplicationContext {
refresh();
}
/**
* Exposes the underlying {@link XmlBeanDefinitionReader} for additional
* configuration facilities and {@code loadBeanDefinition} variations.
*/
public final XmlBeanDefinitionReader getReader() {
return this.reader;
}
/**
* Set whether to use XML validation. Default is {@code true}.
*/
@@ -91,14 +99,13 @@ public class GenericXmlApplicationContext extends GenericApplicationContext {
}
/**
* {@inheritDoc}
* <p>Delegates the given environment to underlying {@link XmlBeanDefinitionReader}.
* Should be called before any call to {@link #load}.
* Delegates the given environment to underlying {@link XmlBeanDefinitionReader}.
* Should be called before any call to {@code #load}.
*/
@Override
public void setEnvironment(ConfigurableEnvironment environment) {
super.setEnvironment(environment);
this.reader.setEnvironment(this.getEnvironment());
this.reader.setEnvironment(getEnvironment());
}
/**