Polish GroovyBeanDefinitionReader formatting
Consistent use of tabs and remove trailing whitespace. Issue: SPR-7123
This commit is contained in:
@@ -45,53 +45,53 @@ class GroovyDynamicElementReader extends GroovyObjectSupport {
|
|||||||
private boolean callAfterInvocation = true
|
private boolean callAfterInvocation = true
|
||||||
|
|
||||||
|
|
||||||
public GroovyDynamicElementReader(String namespace, Map<String, String> namespaceMap,
|
public GroovyDynamicElementReader(String namespace, Map<String, String> namespaceMap,
|
||||||
BeanDefinitionParserDelegate delegate, GroovyBeanDefinitionWrapper beanDefinition, boolean decorating) {
|
BeanDefinitionParserDelegate delegate, GroovyBeanDefinitionWrapper beanDefinition, boolean decorating) {
|
||||||
super();
|
super();
|
||||||
this.rootNamespace = namespace
|
this.rootNamespace = namespace
|
||||||
this.xmlNamespaces = namespaceMap
|
this.xmlNamespaces = namespaceMap
|
||||||
this.delegate = delegate
|
this.delegate = delegate
|
||||||
this.beanDefinition = beanDefinition;
|
this.beanDefinition = beanDefinition;
|
||||||
this.decorating = decorating;
|
this.decorating = decorating;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object invokeMethod(String name, Object args) {
|
public Object invokeMethod(String name, Object args) {
|
||||||
if (name.equals("doCall")) {
|
if (name.equals("doCall")) {
|
||||||
def callable = args[0]
|
def callable = args[0]
|
||||||
callable.resolveStrategy = Closure.DELEGATE_FIRST
|
callable.resolveStrategy = Closure.DELEGATE_FIRST
|
||||||
callable.delegate = this
|
callable.delegate = this
|
||||||
def result = callable.call()
|
def result = callable.call()
|
||||||
|
|
||||||
if (this.callAfterInvocation) {
|
if (this.callAfterInvocation) {
|
||||||
afterInvocation()
|
afterInvocation()
|
||||||
this.callAfterInvocation = false
|
this.callAfterInvocation = false
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
StreamingMarkupBuilder builder = new StreamingMarkupBuilder();
|
StreamingMarkupBuilder builder = new StreamingMarkupBuilder();
|
||||||
def myNamespace = this.rootNamespace
|
def myNamespace = this.rootNamespace
|
||||||
def myNamespaces = this.xmlNamespaces
|
def myNamespaces = this.xmlNamespaces
|
||||||
|
|
||||||
def callable = {
|
def callable = {
|
||||||
for (namespace in myNamespaces) {
|
for (namespace in myNamespaces) {
|
||||||
mkp.declareNamespace([(namespace.key):namespace.value])
|
mkp.declareNamespace([(namespace.key):namespace.value])
|
||||||
}
|
}
|
||||||
if (args && (args[-1] instanceof Closure)) {
|
if (args && (args[-1] instanceof Closure)) {
|
||||||
args[-1].resolveStrategy = Closure.DELEGATE_FIRST
|
args[-1].resolveStrategy = Closure.DELEGATE_FIRST
|
||||||
args[-1].delegate = builder
|
args[-1].delegate = builder
|
||||||
}
|
}
|
||||||
delegate."$myNamespace"."$name"(*args)
|
delegate."$myNamespace"."$name"(*args)
|
||||||
}
|
}
|
||||||
|
|
||||||
callable.resolveStrategy = Closure.DELEGATE_FIRST
|
callable.resolveStrategy = Closure.DELEGATE_FIRST
|
||||||
callable.delegate = builder
|
callable.delegate = builder
|
||||||
def writable = builder.bind(callable)
|
def writable = builder.bind(callable)
|
||||||
def sw = new StringWriter()
|
def sw = new StringWriter()
|
||||||
writable.writeTo(sw)
|
writable.writeTo(sw)
|
||||||
|
|
||||||
Element element = this.delegate.readerContext.readDocumentFromString(sw.toString()).documentElement
|
Element element = this.delegate.readerContext.readDocumentFromString(sw.toString()).documentElement
|
||||||
this.delegate.initDefaults(element)
|
this.delegate.initDefaults(element)
|
||||||
@@ -106,13 +106,13 @@ class GroovyDynamicElementReader extends GroovyObjectSupport {
|
|||||||
this.beanDefinition.setBeanDefinition(beanDefinition)
|
this.beanDefinition.setBeanDefinition(beanDefinition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.callAfterInvocation) {
|
if (this.callAfterInvocation) {
|
||||||
afterInvocation()
|
afterInvocation()
|
||||||
this.callAfterInvocation = false
|
this.callAfterInvocation = false
|
||||||
}
|
}
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook that subclass or anonymous classes can overwrite to implement custom behavior
|
* Hook that subclass or anonymous classes can overwrite to implement custom behavior
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
|
|
||||||
private final Map <String, String> namespaces = new HashMap<String, String>();
|
private final Map <String, String> namespaces = new HashMap<String, String>();
|
||||||
|
|
||||||
private final Map<String, DeferredProperty> deferredProperties = new HashMap<String, DeferredProperty>();
|
private final Map<String, DeferredProperty> deferredProperties = new HashMap<String, DeferredProperty>();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -140,11 +140,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
* @param registry the BeanFactory to load bean definitions into,
|
* @param registry the BeanFactory to load bean definitions into,
|
||||||
* in the form of a BeanDefinitionRegistry
|
* in the form of a BeanDefinitionRegistry
|
||||||
*/
|
*/
|
||||||
public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry) {
|
public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry) {
|
||||||
super(registry);
|
super(registry);
|
||||||
this.xmlBeanDefinitionReader = new XmlBeanDefinitionReader(registry);
|
this.xmlBeanDefinitionReader = new XmlBeanDefinitionReader(registry);
|
||||||
this.xmlBeanDefinitionReader.setValidating(false);
|
this.xmlBeanDefinitionReader.setValidating(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new GroovyBeanDefinitionReader based on the given XmlBeanDefinitionReader,
|
* Create new GroovyBeanDefinitionReader based on the given XmlBeanDefinitionReader,
|
||||||
@@ -293,26 +293,26 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines an Spring namespace definition to use.
|
* Defines an Spring namespace definition to use.
|
||||||
* @param definition the namespace definition
|
* @param definition the namespace definition
|
||||||
*/
|
*/
|
||||||
public void xmlns(Map<String, String> definition) {
|
public void xmlns(Map<String, String> definition) {
|
||||||
if (!definition.isEmpty()) {
|
if (!definition.isEmpty()) {
|
||||||
for (Map.Entry<String,String> entry : definition.entrySet()) {
|
for (Map.Entry<String,String> entry : definition.entrySet()) {
|
||||||
String namespace = entry.getKey();
|
String namespace = entry.getKey();
|
||||||
String uri = entry.getValue();
|
String uri = entry.getValue();
|
||||||
if (uri == null) {
|
if (uri == null) {
|
||||||
throw new IllegalArgumentException("Namespace definition must supply a non-null URI");
|
throw new IllegalArgumentException("Namespace definition must supply a non-null URI");
|
||||||
}
|
}
|
||||||
NamespaceHandler namespaceHandler = this.xmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri);
|
NamespaceHandler namespaceHandler = this.xmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri);
|
||||||
if (namespaceHandler == null) {
|
if (namespaceHandler == null) {
|
||||||
throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri,
|
throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri,
|
||||||
new Location(new DescriptiveResource(("Groovy")))));
|
new Location(new DescriptiveResource(("Groovy")))));
|
||||||
}
|
}
|
||||||
this.namespaces.put(namespace, uri);
|
this.namespaces.put(namespace, uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance.
|
* Imports Spring bean definitions from either XML or Groovy sources into the current bean builder instance.
|
||||||
@@ -335,15 +335,15 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
|
|
||||||
// INTERNAL HANDLING OF GROOVY CLOSURES AND PROPERTIES
|
// INTERNAL HANDLING OF GROOVY CLOSURES AND PROPERTIES
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method overrides method invocation to create beans for each method name that
|
* This method overrides method invocation to create beans for each method name that
|
||||||
* takes a class argument
|
* takes a class argument
|
||||||
*/
|
*/
|
||||||
public Object invokeMethod(String name, Object arg) {
|
public Object invokeMethod(String name, Object arg) {
|
||||||
Object[] args = (Object[])arg;
|
Object[] args = (Object[])arg;
|
||||||
if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
|
if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
|
||||||
return beans((Closure) args[0]);
|
return beans((Closure) args[0]);
|
||||||
}
|
}
|
||||||
else if ("ref".equals(name)) {
|
else if ("ref".equals(name)) {
|
||||||
String refName;
|
String refName;
|
||||||
if (args[0] == null)
|
if (args[0] == null)
|
||||||
@@ -363,13 +363,13 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
}
|
}
|
||||||
return new RuntimeBeanReference(refName, parentRef);
|
return new RuntimeBeanReference(refName, parentRef);
|
||||||
}
|
}
|
||||||
else if (this.namespaces.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) {
|
else if (this.namespaces.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) {
|
||||||
GroovyDynamicElementReader reader = createDynamicElementReader(name);
|
GroovyDynamicElementReader reader = createDynamicElementReader(name);
|
||||||
reader.invokeMethod("doCall", args);
|
reader.invokeMethod("doCall", args);
|
||||||
}
|
}
|
||||||
else if (args.length > 0 && args[0] instanceof Closure) {
|
else if (args.length > 0 && args[0] instanceof Closure) {
|
||||||
// abstract bean definition
|
// abstract bean definition
|
||||||
return invokeBeanDefiningMethod(name, args);
|
return invokeBeanDefiningMethod(name, args);
|
||||||
}
|
}
|
||||||
else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
|
else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
|
||||||
return invokeBeanDefiningMethod(name, args);
|
return invokeBeanDefiningMethod(name, args);
|
||||||
@@ -377,11 +377,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
else if (args.length > 1 && args[args.length -1] instanceof Closure) {
|
else if (args.length > 1 && args[args.length -1] instanceof Closure) {
|
||||||
return invokeBeanDefiningMethod(name, args);
|
return invokeBeanDefiningMethod(name, args);
|
||||||
}
|
}
|
||||||
MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
|
MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
|
||||||
if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
|
if (!mc.respondsTo(getRegistry(), name, args).isEmpty()){
|
||||||
return mc.invokeMethod(getRegistry(), name, args);
|
return mc.invokeMethod(getRegistry(), name, args);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean addDeferredProperty(String property, Object newValue) {
|
private boolean addDeferredProperty(String property, Object newValue) {
|
||||||
@@ -431,13 +431,13 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
* @return the bean definition wrapper
|
* @return the bean definition wrapper
|
||||||
*/
|
*/
|
||||||
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
|
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
|
||||||
boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
|
boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
|
||||||
if (args[0] instanceof Class) {
|
if (args[0] instanceof Class) {
|
||||||
Class beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass());
|
Class beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass());
|
||||||
if (args.length >= 1) {
|
if (args.length >= 1) {
|
||||||
if (hasClosureArgument) {
|
if (hasClosureArgument) {
|
||||||
if (args.length-1 != 1) {
|
if (args.length-1 != 1) {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(
|
||||||
beanName, beanClass, resolveConstructorArguments(args,1,args.length-1));
|
beanName, beanClass, resolveConstructorArguments(args,1,args.length-1));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -456,77 +456,77 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName());
|
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName());
|
||||||
}
|
}
|
||||||
else if (args[0] instanceof Map) {
|
else if (args[0] instanceof Map) {
|
||||||
// named constructor arguments
|
// named constructor arguments
|
||||||
if (args.length > 1 && args[1] instanceof Class) {
|
if (args.length > 1 && args[1] instanceof Class) {
|
||||||
List constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length-1 : args.length);
|
List constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length-1 : args.length);
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class)args[1], constructorArgs);
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class)args[1], constructorArgs);
|
||||||
Map namedArgs = (Map)args[0];
|
Map namedArgs = (Map)args[0];
|
||||||
for (Object o : namedArgs.keySet()) {
|
for (Object o : namedArgs.keySet()) {
|
||||||
String propName = (String) o;
|
String propName = (String) o;
|
||||||
setProperty(propName, namedArgs.get(propName));
|
setProperty(propName, namedArgs.get(propName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// factory method syntax
|
// factory method syntax
|
||||||
else {
|
else {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
|
||||||
//First arg is the map containing factoryBean : factoryMethod
|
//First arg is the map containing factoryBean : factoryMethod
|
||||||
Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next();
|
Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next();
|
||||||
// If we have a closure body, that will be the last argument.
|
// If we have a closure body, that will be the last argument.
|
||||||
// In between are the constructor args
|
// In between are the constructor args
|
||||||
int constructorArgsTest = hasClosureArgument?2:1;
|
int constructorArgsTest = hasClosureArgument?2:1;
|
||||||
// If we have more than this number of args, we have constructor args
|
// If we have more than this number of args, we have constructor args
|
||||||
if (args.length > constructorArgsTest){
|
if (args.length > constructorArgsTest){
|
||||||
// factory-method requires args
|
// factory-method requires args
|
||||||
int endOfConstructArgs = (hasClosureArgument? args.length - 1 : args.length);
|
int endOfConstructArgs = (hasClosureArgument? args.length - 1 : args.length);
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null,
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null,
|
||||||
resolveConstructorArguments(args, 1, endOfConstructArgs));
|
resolveConstructorArguments(args, 1, endOfConstructArgs));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
|
||||||
}
|
}
|
||||||
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(factoryBeanEntry.getKey().toString());
|
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(factoryBeanEntry.getKey().toString());
|
||||||
this.currentBeanDefinition.getBeanDefinition().setFactoryMethodName(factoryBeanEntry.getValue().toString());
|
this.currentBeanDefinition.getBeanDefinition().setFactoryMethodName(factoryBeanEntry.getValue().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (args[0] instanceof Closure) {
|
else if (args[0] instanceof Closure) {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
|
||||||
this.currentBeanDefinition.getBeanDefinition().setAbstract(true);
|
this.currentBeanDefinition.getBeanDefinition().setAbstract(true);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length-1 : args.length);
|
List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length-1 : args.length);
|
||||||
currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs);
|
currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs);
|
||||||
}
|
|
||||||
|
|
||||||
if (hasClosureArgument) {
|
|
||||||
Closure callable = (Closure)args[args.length-1];
|
|
||||||
callable.setDelegate(this);
|
|
||||||
callable.setResolveStrategy(Closure.DELEGATE_FIRST);
|
|
||||||
callable.call(new Object[]{currentBeanDefinition});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GroovyBeanDefinitionWrapper beanDefinition = currentBeanDefinition;
|
if (hasClosureArgument) {
|
||||||
|
Closure callable = (Closure)args[args.length-1];
|
||||||
|
callable.setDelegate(this);
|
||||||
|
callable.setResolveStrategy(Closure.DELEGATE_FIRST);
|
||||||
|
callable.call(new Object[]{currentBeanDefinition});
|
||||||
|
}
|
||||||
|
|
||||||
|
GroovyBeanDefinitionWrapper beanDefinition = currentBeanDefinition;
|
||||||
this.currentBeanDefinition = null;
|
this.currentBeanDefinition = null;
|
||||||
beanDefinition.getBeanDefinition().setAttribute(GroovyBeanDefinitionWrapper.class.getName(), beanDefinition);
|
beanDefinition.getBeanDefinition().setAttribute(GroovyBeanDefinitionWrapper.class.getName(), beanDefinition);
|
||||||
getRegistry().registerBeanDefinition(beanName, beanDefinition.getBeanDefinition());
|
getRegistry().registerBeanDefinition(beanName, beanDefinition.getBeanDefinition());
|
||||||
return beanDefinition;
|
return beanDefinition;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Object> resolveConstructorArguments(Object[] args, int start, int end) {
|
protected List<Object> resolveConstructorArguments(Object[] args, int start, int end) {
|
||||||
Object[] constructorArgs = Arrays.copyOfRange(args, start, end);
|
Object[] constructorArgs = Arrays.copyOfRange(args, start, end);
|
||||||
for (int i = 0; i < constructorArgs.length; i++) {
|
for (int i = 0; i < constructorArgs.length; i++) {
|
||||||
if (constructorArgs[i] instanceof GString) {
|
if (constructorArgs[i] instanceof GString) {
|
||||||
constructorArgs[i] = constructorArgs[i].toString();
|
constructorArgs[i] = constructorArgs[i].toString();
|
||||||
}
|
}
|
||||||
else if (constructorArgs[i] instanceof List) {
|
else if (constructorArgs[i] instanceof List) {
|
||||||
constructorArgs[i] = manageListIfNecessary((List) constructorArgs[i]);
|
constructorArgs[i] = manageListIfNecessary((List) constructorArgs[i]);
|
||||||
}
|
}
|
||||||
else if (constructorArgs[i] instanceof Map){
|
else if (constructorArgs[i] instanceof Map){
|
||||||
constructorArgs[i] = manageMapIfNecessary((Map) constructorArgs[i]);
|
constructorArgs[i] = manageMapIfNecessary((Map) constructorArgs[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Arrays.asList(constructorArgs);
|
return Arrays.asList(constructorArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether there are any {@link RuntimeBeanReference} inside the Map
|
* Checks whether there are any {@link RuntimeBeanReference} inside the Map
|
||||||
@@ -578,39 +578,39 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
*/
|
*/
|
||||||
public void setProperty(String name, Object value) {
|
public void setProperty(String name, Object value) {
|
||||||
if (this.currentBeanDefinition != null) {
|
if (this.currentBeanDefinition != null) {
|
||||||
applyPropertyToBeanDefinition(name, value);
|
applyPropertyToBeanDefinition(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void applyPropertyToBeanDefinition(String name, Object value) {
|
protected void applyPropertyToBeanDefinition(String name, Object value) {
|
||||||
if (value instanceof GString) {
|
if (value instanceof GString) {
|
||||||
value = value.toString();
|
value = value.toString();
|
||||||
}
|
}
|
||||||
if (addDeferredProperty(name, value)) {
|
if (addDeferredProperty(name, value)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (value instanceof Closure) {
|
else if (value instanceof Closure) {
|
||||||
GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
|
GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
|
||||||
try {
|
try {
|
||||||
Closure callable = (Closure) value;
|
Closure callable = (Closure) value;
|
||||||
Class parameterType = callable.getParameterTypes()[0];
|
Class parameterType = callable.getParameterTypes()[0];
|
||||||
if (parameterType.equals(Object.class)) {
|
if (parameterType.equals(Object.class)) {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
|
||||||
callable.call(this.currentBeanDefinition);
|
callable.call(this.currentBeanDefinition);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, parameterType);
|
||||||
callable.call((Object) null);
|
callable.call((Object) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
value = this.currentBeanDefinition.getBeanDefinition();
|
value = this.currentBeanDefinition.getBeanDefinition();
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
this.currentBeanDefinition = current;
|
this.currentBeanDefinition = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.currentBeanDefinition.addProperty(name, value);
|
this.currentBeanDefinition.addProperty(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method overrides property retrieval in the scope of the GroovyBeanDefinitionReader to either:
|
* This method overrides property retrieval in the scope of the GroovyBeanDefinitionReader to either:
|
||||||
@@ -624,9 +624,9 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
return binding.getVariable(name);
|
return binding.getVariable(name);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (this.namespaces.containsKey(name)) {
|
if (this.namespaces.containsKey(name)) {
|
||||||
return createDynamicElementReader(name);
|
return createDynamicElementReader(name);
|
||||||
}
|
}
|
||||||
if (getRegistry().containsBeanDefinition(name)) {
|
if (getRegistry().containsBeanDefinition(name)) {
|
||||||
GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
|
GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
|
||||||
getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
|
getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
|
||||||
@@ -660,22 +660,22 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
|
private GroovyDynamicElementReader createDynamicElementReader(String namespace) {
|
||||||
XmlReaderContext readerContext = this.xmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy"));
|
XmlReaderContext readerContext = this.xmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy"));
|
||||||
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment());
|
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment());
|
||||||
boolean decorating = (this.currentBeanDefinition != null);
|
boolean decorating = (this.currentBeanDefinition != null);
|
||||||
if (!decorating) {
|
if (!decorating) {
|
||||||
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
|
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace);
|
||||||
}
|
}
|
||||||
return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
|
return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) {
|
||||||
@Override
|
@Override
|
||||||
protected void afterInvocation() {
|
protected void afterInvocation() {
|
||||||
if (!this.decorating) {
|
if (!this.decorating) {
|
||||||
currentBeanDefinition = null;
|
currentBeanDefinition = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
|
|||||||
private static final String FACTORY_METHOD = "factoryMethod";
|
private static final String FACTORY_METHOD = "factoryMethod";
|
||||||
private static final String INIT_METHOD = "initMethod";
|
private static final String INIT_METHOD = "initMethod";
|
||||||
private static final String DESTROY_METHOD = "destroyMethod";
|
private static final String DESTROY_METHOD = "destroyMethod";
|
||||||
private static final String SINGLETON = "singleton";
|
private static final String SINGLETON = "singleton";
|
||||||
|
|
||||||
private static final List<String> dynamicProperties = new ArrayList<String>(8);
|
private static final List<String> dynamicProperties = new ArrayList<String>(8);
|
||||||
|
|
||||||
@@ -173,69 +173,69 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setProperty(String property, Object newValue) {
|
public void setProperty(String property, Object newValue) {
|
||||||
if (PARENT.equals(property)) {
|
if (PARENT.equals(property)) {
|
||||||
setParent(newValue);
|
setParent(newValue);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
AbstractBeanDefinition bd = getBeanDefinition();
|
AbstractBeanDefinition bd = getBeanDefinition();
|
||||||
if (AUTOWIRE.equals(property)) {
|
if (AUTOWIRE.equals(property)) {
|
||||||
if ("byName".equals(newValue)) {
|
if ("byName".equals(newValue)) {
|
||||||
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
|
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
|
||||||
}
|
}
|
||||||
else if ("byType".equals(newValue)) {
|
else if ("byType".equals(newValue)) {
|
||||||
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
|
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
|
||||||
}
|
}
|
||||||
else if ("constructor".equals(newValue)) {
|
else if ("constructor".equals(newValue)) {
|
||||||
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
|
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
|
||||||
}
|
}
|
||||||
else if (Boolean.TRUE.equals(newValue)) {
|
else if (Boolean.TRUE.equals(newValue)) {
|
||||||
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
|
bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// constructorArgs
|
// constructorArgs
|
||||||
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
|
else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
|
||||||
ConstructorArgumentValues cav = new ConstructorArgumentValues();
|
ConstructorArgumentValues cav = new ConstructorArgumentValues();
|
||||||
List args = (List) newValue;
|
List args = (List) newValue;
|
||||||
for (Object arg : args) {
|
for (Object arg : args) {
|
||||||
cav.addGenericArgumentValue(arg);
|
cav.addGenericArgumentValue(arg);
|
||||||
}
|
}
|
||||||
bd.setConstructorArgumentValues(cav);
|
bd.setConstructorArgumentValues(cav);
|
||||||
}
|
}
|
||||||
// factoryBean
|
// factoryBean
|
||||||
else if (FACTORY_BEAN.equals(property)) {
|
else if (FACTORY_BEAN.equals(property)) {
|
||||||
if (newValue != null) {
|
if (newValue != null) {
|
||||||
bd.setFactoryBeanName(newValue.toString());
|
bd.setFactoryBeanName(newValue.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// factoryMethod
|
// factoryMethod
|
||||||
else if (FACTORY_METHOD.equals(property)) {
|
else if (FACTORY_METHOD.equals(property)) {
|
||||||
if (newValue != null)
|
if (newValue != null)
|
||||||
bd.setFactoryMethodName(newValue.toString());
|
bd.setFactoryMethodName(newValue.toString());
|
||||||
}
|
}
|
||||||
// initMethod
|
// initMethod
|
||||||
else if (INIT_METHOD.equals(property)) {
|
else if (INIT_METHOD.equals(property)) {
|
||||||
if (newValue != null) {
|
if (newValue != null) {
|
||||||
bd.setInitMethodName(newValue.toString());
|
bd.setInitMethodName(newValue.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// destroyMethod
|
// destroyMethod
|
||||||
else if (DESTROY_METHOD.equals(property)) {
|
else if (DESTROY_METHOD.equals(property)) {
|
||||||
if (newValue != null) {
|
if (newValue != null) {
|
||||||
bd.setDestroyMethodName(newValue.toString());
|
bd.setDestroyMethodName(newValue.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// singleton property
|
// singleton property
|
||||||
else if (SINGLETON.equals(property)) {
|
else if (SINGLETON.equals(property)) {
|
||||||
bd.setScope(Boolean.TRUE.equals(newValue) ?
|
bd.setScope(Boolean.TRUE.equals(newValue) ?
|
||||||
BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
|
BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
|
||||||
}
|
}
|
||||||
else if (this.definitionWrapper.isWritableProperty(property)) {
|
else if (this.definitionWrapper.isWritableProperty(property)) {
|
||||||
this.definitionWrapper.setPropertyValue(property, newValue);
|
this.definitionWrapper.setPropertyValue(property, newValue);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
super.setProperty(property, newValue);
|
super.setProperty(property, newValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,26 +24,26 @@ import org.springframework.context.support.GenericGroovyApplicationContext
|
|||||||
*/
|
*/
|
||||||
class GroovyApplicationContextDynamicBeanPropertyTests extends GroovyTestCase {
|
class GroovyApplicationContextDynamicBeanPropertyTests extends GroovyTestCase {
|
||||||
|
|
||||||
void testAccessDynamicBeanProperties() {
|
void testAccessDynamicBeanProperties() {
|
||||||
def ctx = new GenericGroovyApplicationContext();
|
|
||||||
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
|
|
||||||
ctx.refresh()
|
|
||||||
|
|
||||||
def framework = ctx.framework
|
|
||||||
assertNotNull 'could not find framework bean', framework
|
|
||||||
assertEquals 'Grails', framework
|
|
||||||
}
|
|
||||||
|
|
||||||
void testAccessingNonExistentBeanViaDynamicProperty() {
|
|
||||||
def ctx = new GenericGroovyApplicationContext();
|
def ctx = new GenericGroovyApplicationContext();
|
||||||
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
|
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
|
||||||
ctx.refresh()
|
ctx.refresh()
|
||||||
|
|
||||||
def err = shouldFail(NoSuchBeanDefinitionException) {
|
def framework = ctx.framework
|
||||||
ctx.someNonExistentBean
|
assertNotNull 'could not find framework bean', framework
|
||||||
}
|
assertEquals 'Grails', framework
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals "No bean named 'someNonExistentBean' is defined", err
|
void testAccessingNonExistentBeanViaDynamicProperty() {
|
||||||
}
|
def ctx = new GenericGroovyApplicationContext();
|
||||||
|
ctx.reader.loadBeanDefinitions("org/springframework/context/groovy/applicationContext.groovy");
|
||||||
|
ctx.refresh()
|
||||||
|
|
||||||
|
def err = shouldFail(NoSuchBeanDefinitionException) {
|
||||||
|
ctx.someNonExistentBean
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals "No bean named 'someNonExistentBean' is defined", err
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,247 +57,247 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
assertEquals "hello", foo
|
assertEquals "hello", foo
|
||||||
}
|
}
|
||||||
|
|
||||||
void testSingletonPropertyOnBeanDefinition() {
|
void testSingletonPropertyOnBeanDefinition() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
singletonBean(Bean1) { bean ->
|
singletonBean(Bean1) { bean ->
|
||||||
bean.singleton = true
|
bean.singleton = true
|
||||||
}
|
}
|
||||||
nonSingletonBean(Bean1) { bean ->
|
nonSingletonBean(Bean1) { bean ->
|
||||||
bean.singleton = false
|
bean.singleton = false
|
||||||
}
|
}
|
||||||
unSpecifiedScopeBean(Bean1)
|
unSpecifiedScopeBean(Bean1)
|
||||||
}
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
assertTrue 'singletonBean should have been a singleton', appCtx.isSingleton('singletonBean')
|
|
||||||
assertFalse 'nonSingletonBean should not have been a singleton', appCtx.isSingleton('nonSingletonBean')
|
|
||||||
assertTrue 'unSpecifiedScopeBean should not have been a singleton', appCtx.isSingleton('unSpecifiedScopeBean')
|
|
||||||
}
|
|
||||||
|
|
||||||
void testInheritPropertiesFromAbstractBean() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
|
|
||||||
reader.beans {
|
|
||||||
myB(Bean1){
|
|
||||||
person = "wombat"
|
|
||||||
}
|
|
||||||
|
|
||||||
myAbstractA(Bean2){ bean ->
|
|
||||||
bean.'abstract' = true
|
|
||||||
age = 10
|
|
||||||
bean1 = myB
|
|
||||||
}
|
|
||||||
myConcreteB {
|
|
||||||
it.parent = myAbstractA
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
def bean = appCtx.getBean("myConcreteB")
|
|
||||||
assertEquals 10, bean.age
|
|
||||||
assertNotNull bean.bean1
|
|
||||||
}
|
|
||||||
|
|
||||||
void testContextComponentScanSpringTag() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
|
|
||||||
reader.beans {
|
|
||||||
xmlns context:"http://www.springframework.org/schema/context"
|
|
||||||
|
|
||||||
context.'component-scan'( 'base-package' :" org.springframework.context.groovy" )
|
|
||||||
}
|
|
||||||
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
def p = appCtx.getBean("person")
|
|
||||||
assertTrue(p instanceof AdvisedPerson)
|
|
||||||
assertNotNull p
|
|
||||||
}
|
|
||||||
|
|
||||||
void testUseSpringNamespaceAsMethod() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
|
|
||||||
reader.beans {
|
|
||||||
xmlns aop:"http://www.springframework.org/schema/aop"
|
|
||||||
|
|
||||||
fred(AdvisedPerson) {
|
|
||||||
name = "Fred"
|
|
||||||
age = 45
|
|
||||||
}
|
|
||||||
birthdayCardSenderAspect(BirthdayCardSender)
|
|
||||||
|
|
||||||
aop {
|
|
||||||
config("proxy-target-class":true) {
|
|
||||||
aspect( id:"sendBirthdayCard",ref:"birthdayCardSenderAspect" ) {
|
|
||||||
after method:"onBirthday", pointcut: "execution(void org.springframework.context.groovy.AdvisedPerson.birthday()) and this(person)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
def fred = appCtx.getBean("fred")
|
|
||||||
assertTrue (fred instanceof SpringProxy)
|
|
||||||
fred.birthday()
|
|
||||||
|
|
||||||
BirthdayCardSender birthDaySender = appCtx.getBean("birthdayCardSenderAspect")
|
|
||||||
|
|
||||||
assertEquals 1, birthDaySender.peopleSentCards.size()
|
|
||||||
assertEquals "Fred", birthDaySender.peopleSentCards[0].name
|
|
||||||
}
|
|
||||||
|
|
||||||
void testUseTwoSpringNamespaces() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
TestScope scope = new TestScope()
|
|
||||||
appCtx.getBeanFactory().registerScope("test", scope)
|
|
||||||
|
|
||||||
reader.beans {
|
|
||||||
xmlns aop:"http://www.springframework.org/schema/aop"
|
|
||||||
xmlns util:"http://www.springframework.org/schema/util"
|
|
||||||
scopedList(ArrayList) { bean ->
|
|
||||||
bean.scope = "test"
|
|
||||||
aop.'scoped-proxy'()
|
|
||||||
}
|
|
||||||
util.list(id: 'foo') {
|
|
||||||
value 'one'
|
|
||||||
value 'two'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
|
|
||||||
assert ['one', 'two'] == appCtx.getBean("foo")
|
assertTrue 'singletonBean should have been a singleton', appCtx.isSingleton('singletonBean')
|
||||||
|
assertFalse 'nonSingletonBean should not have been a singleton', appCtx.isSingleton('nonSingletonBean')
|
||||||
|
assertTrue 'unSpecifiedScopeBean should not have been a singleton', appCtx.isSingleton('unSpecifiedScopeBean')
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull appCtx.getBean("scopedList")
|
void testInheritPropertiesFromAbstractBean() {
|
||||||
assertNotNull appCtx.getBean("scopedList").size()
|
def appCtx = new GenericApplicationContext()
|
||||||
assertNotNull appCtx.getBean("scopedList").size()
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
// should only be true because bean not initialized until proxy called
|
reader.beans {
|
||||||
assertEquals 2, scope.instanceCount
|
myB(Bean1){
|
||||||
|
person = "wombat"
|
||||||
|
}
|
||||||
|
|
||||||
|
myAbstractA(Bean2){ bean ->
|
||||||
|
bean.'abstract' = true
|
||||||
|
age = 10
|
||||||
|
bean1 = myB
|
||||||
|
}
|
||||||
|
myConcreteB {
|
||||||
|
it.parent = myAbstractA
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
def bean = appCtx.getBean("myConcreteB")
|
||||||
|
assertEquals 10, bean.age
|
||||||
|
assertNotNull bean.bean1
|
||||||
|
}
|
||||||
|
|
||||||
|
void testContextComponentScanSpringTag() {
|
||||||
|
def appCtx = new GenericApplicationContext()
|
||||||
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
|
reader.beans {
|
||||||
|
xmlns context:"http://www.springframework.org/schema/context"
|
||||||
|
|
||||||
|
context.'component-scan'( 'base-package' :" org.springframework.context.groovy" )
|
||||||
|
}
|
||||||
|
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
def p = appCtx.getBean("person")
|
||||||
|
assertTrue(p instanceof AdvisedPerson)
|
||||||
|
assertNotNull p
|
||||||
|
}
|
||||||
|
|
||||||
|
void testUseSpringNamespaceAsMethod() {
|
||||||
|
def appCtx = new GenericApplicationContext()
|
||||||
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
|
reader.beans {
|
||||||
|
xmlns aop:"http://www.springframework.org/schema/aop"
|
||||||
|
|
||||||
|
fred(AdvisedPerson) {
|
||||||
|
name = "Fred"
|
||||||
|
age = 45
|
||||||
|
}
|
||||||
|
birthdayCardSenderAspect(BirthdayCardSender)
|
||||||
|
|
||||||
|
aop {
|
||||||
|
config("proxy-target-class":true) {
|
||||||
|
aspect( id:"sendBirthdayCard",ref:"birthdayCardSenderAspect" ) {
|
||||||
|
after method:"onBirthday", pointcut: "execution(void org.springframework.context.groovy.AdvisedPerson.birthday()) and this(person)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
def fred = appCtx.getBean("fred")
|
||||||
|
assertTrue (fred instanceof SpringProxy)
|
||||||
|
fred.birthday()
|
||||||
|
|
||||||
|
BirthdayCardSender birthDaySender = appCtx.getBean("birthdayCardSenderAspect")
|
||||||
|
|
||||||
|
assertEquals 1, birthDaySender.peopleSentCards.size()
|
||||||
|
assertEquals "Fred", birthDaySender.peopleSentCards[0].name
|
||||||
|
}
|
||||||
|
|
||||||
|
void testUseTwoSpringNamespaces() {
|
||||||
|
def appCtx = new GenericApplicationContext()
|
||||||
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
TestScope scope = new TestScope()
|
||||||
|
appCtx.getBeanFactory().registerScope("test", scope)
|
||||||
|
|
||||||
|
reader.beans {
|
||||||
|
xmlns aop:"http://www.springframework.org/schema/aop"
|
||||||
|
xmlns util:"http://www.springframework.org/schema/util"
|
||||||
|
scopedList(ArrayList) { bean ->
|
||||||
|
bean.scope = "test"
|
||||||
|
aop.'scoped-proxy'()
|
||||||
|
}
|
||||||
|
util.list(id: 'foo') {
|
||||||
|
value 'one'
|
||||||
|
value 'two'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
assert ['one', 'two'] == appCtx.getBean("foo")
|
||||||
|
|
||||||
|
assertNotNull appCtx.getBean("scopedList")
|
||||||
|
assertNotNull appCtx.getBean("scopedList").size()
|
||||||
|
assertNotNull appCtx.getBean("scopedList").size()
|
||||||
|
|
||||||
|
// should only be true because bean not initialized until proxy called
|
||||||
|
assertEquals 2, scope.instanceCount
|
||||||
|
|
||||||
appCtx = new GenericApplicationContext()
|
appCtx = new GenericApplicationContext()
|
||||||
reader = new GroovyBeanDefinitionReader(appCtx)
|
reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
appCtx.getBeanFactory().registerScope("test", scope)
|
appCtx.getBeanFactory().registerScope("test", scope)
|
||||||
|
|
||||||
reader.beans {
|
reader.beans {
|
||||||
xmlns aop:"http://www.springframework.org/schema/aop",
|
xmlns aop:"http://www.springframework.org/schema/aop",
|
||||||
util:"http://www.springframework.org/schema/util"
|
util:"http://www.springframework.org/schema/util"
|
||||||
scopedList(ArrayList) { bean ->
|
scopedList(ArrayList) { bean ->
|
||||||
bean.scope = "test"
|
bean.scope = "test"
|
||||||
aop.'scoped-proxy'()
|
aop.'scoped-proxy'()
|
||||||
}
|
}
|
||||||
util.list(id: 'foo') {
|
util.list(id: 'foo') {
|
||||||
value 'one'
|
value 'one'
|
||||||
value 'two'
|
value 'two'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
assert ['one', 'two'] == appCtx.getBean("foo")
|
|
||||||
|
|
||||||
assertNotNull appCtx.getBean("scopedList")
|
|
||||||
assertNotNull appCtx.getBean("scopedList").size()
|
|
||||||
assertNotNull appCtx.getBean("scopedList").size()
|
|
||||||
|
|
||||||
// should only be true because bean not initialized until proxy called
|
|
||||||
assertEquals 4, scope.instanceCount
|
|
||||||
}
|
|
||||||
|
|
||||||
void testSpringAopSupport() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
|
|
||||||
reader.beans {
|
|
||||||
xmlns aop:"http://www.springframework.org/schema/aop"
|
|
||||||
|
|
||||||
fred(AdvisedPerson) {
|
|
||||||
name = "Fred"
|
|
||||||
age = 45
|
|
||||||
}
|
|
||||||
birthdayCardSenderAspect(BirthdayCardSender)
|
|
||||||
|
|
||||||
aop.config("proxy-target-class":true) {
|
|
||||||
aspect( id:"sendBirthdayCard",ref:"birthdayCardSenderAspect" ) {
|
|
||||||
after method:"onBirthday", pointcut: "execution(void org.springframework.context.groovy.AdvisedPerson.birthday()) and this(person)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
def fred = appCtx.getBean("fred")
|
|
||||||
assertTrue (fred instanceof SpringProxy)
|
|
||||||
fred.birthday()
|
|
||||||
|
|
||||||
BirthdayCardSender birthDaySender = appCtx.getBean("birthdayCardSenderAspect")
|
|
||||||
|
|
||||||
assertEquals 1, birthDaySender.peopleSentCards.size()
|
|
||||||
assertEquals "Fred", birthDaySender.peopleSentCards[0].name
|
|
||||||
}
|
|
||||||
|
|
||||||
void testSpringScopedProxyBean() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
|
|
||||||
TestScope scope = new TestScope()
|
|
||||||
appCtx.getBeanFactory().registerScope("test", scope)
|
|
||||||
reader.beans {
|
|
||||||
xmlns aop:"http://www.springframework.org/schema/aop"
|
|
||||||
scopedList(ArrayList) { bean ->
|
|
||||||
bean.scope = "test"
|
|
||||||
aop.'scoped-proxy'()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
assertNotNull appCtx.getBean("scopedList")
|
|
||||||
assertNotNull appCtx.getBean("scopedList").size()
|
|
||||||
assertNotNull appCtx.getBean("scopedList").size()
|
|
||||||
|
|
||||||
// should only be true because bean not initialized until proxy called
|
|
||||||
assertEquals 2, scope.instanceCount
|
|
||||||
}
|
|
||||||
|
|
||||||
void testSpringNamespaceBean() {
|
|
||||||
def appCtx = new GenericApplicationContext()
|
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
|
||||||
reader.beans {
|
|
||||||
xmlns util: 'http://www.springframework.org/schema/util'
|
|
||||||
util.list(id: 'foo') {
|
|
||||||
value 'one'
|
|
||||||
value 'two'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
|
|
||||||
assert ['one', 'two'] == appCtx.getBean('foo')
|
assert ['one', 'two'] == appCtx.getBean("foo")
|
||||||
}
|
|
||||||
|
|
||||||
void testNamedArgumentConstructor() {
|
assertNotNull appCtx.getBean("scopedList")
|
||||||
|
assertNotNull appCtx.getBean("scopedList").size()
|
||||||
|
assertNotNull appCtx.getBean("scopedList").size()
|
||||||
|
|
||||||
|
// should only be true because bean not initialized until proxy called
|
||||||
|
assertEquals 4, scope.instanceCount
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSpringAopSupport() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
|
||||||
holyGrail(HolyGrailQuest)
|
|
||||||
knights(KnightOfTheRoundTable, "Camelot", leader:"lancelot", quest: holyGrail)
|
|
||||||
}
|
|
||||||
appCtx.refresh()
|
|
||||||
|
|
||||||
KnightOfTheRoundTable knights = appCtx.getBean("knights")
|
reader.beans {
|
||||||
HolyGrailQuest quest = appCtx.getBean("holyGrail")
|
xmlns aop:"http://www.springframework.org/schema/aop"
|
||||||
|
|
||||||
assertEquals "Camelot", knights.name
|
fred(AdvisedPerson) {
|
||||||
assertEquals "lancelot", knights.leader
|
name = "Fred"
|
||||||
assertEquals quest, knights.quest
|
age = 45
|
||||||
}
|
}
|
||||||
|
birthdayCardSenderAspect(BirthdayCardSender)
|
||||||
|
|
||||||
void testAbstractBeanDefinition() {
|
aop.config("proxy-target-class":true) {
|
||||||
|
aspect( id:"sendBirthdayCard",ref:"birthdayCardSenderAspect" ) {
|
||||||
|
after method:"onBirthday", pointcut: "execution(void org.springframework.context.groovy.AdvisedPerson.birthday()) and this(person)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
def fred = appCtx.getBean("fred")
|
||||||
|
assertTrue (fred instanceof SpringProxy)
|
||||||
|
fred.birthday()
|
||||||
|
|
||||||
|
BirthdayCardSender birthDaySender = appCtx.getBean("birthdayCardSenderAspect")
|
||||||
|
|
||||||
|
assertEquals 1, birthDaySender.peopleSentCards.size()
|
||||||
|
assertEquals "Fred", birthDaySender.peopleSentCards[0].name
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSpringScopedProxyBean() {
|
||||||
|
def appCtx = new GenericApplicationContext()
|
||||||
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
|
TestScope scope = new TestScope()
|
||||||
|
appCtx.getBeanFactory().registerScope("test", scope)
|
||||||
|
reader.beans {
|
||||||
|
xmlns aop:"http://www.springframework.org/schema/aop"
|
||||||
|
scopedList(ArrayList) { bean ->
|
||||||
|
bean.scope = "test"
|
||||||
|
aop.'scoped-proxy'()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
assertNotNull appCtx.getBean("scopedList")
|
||||||
|
assertNotNull appCtx.getBean("scopedList").size()
|
||||||
|
assertNotNull appCtx.getBean("scopedList").size()
|
||||||
|
|
||||||
|
// should only be true because bean not initialized until proxy called
|
||||||
|
assertEquals 2, scope.instanceCount
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSpringNamespaceBean() {
|
||||||
|
def appCtx = new GenericApplicationContext()
|
||||||
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
reader.beans {
|
||||||
|
xmlns util: 'http://www.springframework.org/schema/util'
|
||||||
|
util.list(id: 'foo') {
|
||||||
|
value 'one'
|
||||||
|
value 'two'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
assert ['one', 'two'] == appCtx.getBean('foo')
|
||||||
|
}
|
||||||
|
|
||||||
|
void testNamedArgumentConstructor() {
|
||||||
|
def appCtx = new GenericApplicationContext()
|
||||||
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
reader.beans {
|
||||||
|
holyGrail(HolyGrailQuest)
|
||||||
|
knights(KnightOfTheRoundTable, "Camelot", leader:"lancelot", quest: holyGrail)
|
||||||
|
}
|
||||||
|
appCtx.refresh()
|
||||||
|
|
||||||
|
KnightOfTheRoundTable knights = appCtx.getBean("knights")
|
||||||
|
HolyGrailQuest quest = appCtx.getBean("holyGrail")
|
||||||
|
|
||||||
|
assertEquals "Camelot", knights.name
|
||||||
|
assertEquals "lancelot", knights.leader
|
||||||
|
assertEquals quest, knights.quest
|
||||||
|
}
|
||||||
|
|
||||||
|
void testAbstractBeanDefinition() {
|
||||||
def appCtx = new GenericGroovyApplicationContext()
|
def appCtx = new GenericGroovyApplicationContext()
|
||||||
appCtx.reader.beans {
|
appCtx.reader.beans {
|
||||||
abstractBean {
|
abstractBean {
|
||||||
@@ -317,9 +317,9 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
appCtx.abstractBean
|
appCtx.abstractBean
|
||||||
}
|
}
|
||||||
assertEquals "Lancelot", knights.leader
|
assertEquals "Lancelot", knights.leader
|
||||||
}
|
}
|
||||||
|
|
||||||
void testAbstractBeanDefinitionWithClass() {
|
void testAbstractBeanDefinitionWithClass() {
|
||||||
def appCtx = new GenericGroovyApplicationContext()
|
def appCtx = new GenericGroovyApplicationContext()
|
||||||
appCtx.reader.beans {
|
appCtx.reader.beans {
|
||||||
abstractBean(KnightOfTheRoundTable) { bean ->
|
abstractBean(KnightOfTheRoundTable) { bean ->
|
||||||
@@ -340,30 +340,30 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
def knights = appCtx.knights
|
def knights = appCtx.knights
|
||||||
assert knights
|
assert knights
|
||||||
assertEquals "Lancelot", knights.leader
|
assertEquals "Lancelot", knights.leader
|
||||||
}
|
}
|
||||||
|
|
||||||
void testScopes() {
|
void testScopes() {
|
||||||
def appCtx = new GenericGroovyApplicationContext()
|
def appCtx = new GenericGroovyApplicationContext()
|
||||||
appCtx.reader.beans {
|
appCtx.reader.beans {
|
||||||
myBean(ScopeTest) { bean ->
|
myBean(ScopeTest) { bean ->
|
||||||
bean.scope = "prototype"
|
bean.scope = "prototype"
|
||||||
}
|
}
|
||||||
myBean2(ScopeTest)
|
myBean2(ScopeTest)
|
||||||
}
|
}
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
|
|
||||||
def b1 = appCtx.myBean
|
def b1 = appCtx.myBean
|
||||||
def b2 = appCtx.myBean
|
def b2 = appCtx.myBean
|
||||||
|
|
||||||
assert b1 != b2
|
assert b1 != b2
|
||||||
|
|
||||||
b1 = appCtx.myBean2
|
b1 = appCtx.myBean2
|
||||||
b2 = appCtx.myBean2
|
b2 = appCtx.myBean2
|
||||||
|
|
||||||
assertEquals b1, b2
|
assertEquals b1, b2
|
||||||
}
|
}
|
||||||
|
|
||||||
void testSimpleBean() {
|
void testSimpleBean() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
@@ -430,10 +430,10 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
marge(Bean2) {
|
marge(Bean2) {
|
||||||
person = "marge"
|
person = "marge"
|
||||||
bean1 = { Bean1 b ->
|
bean1 = { Bean1 b ->
|
||||||
person = "homer"
|
person = "homer"
|
||||||
age = 45
|
age = 45
|
||||||
props = [overweight:true, height:"1.8m"]
|
props = [overweight:true, height:"1.8m"]
|
||||||
children = ["bart", "lisa"] }
|
children = ["bart", "lisa"] }
|
||||||
children = [bart, lisa]
|
children = [bart, lisa]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -558,7 +558,7 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
age = 45
|
age = 45
|
||||||
}
|
}
|
||||||
marge(Bean4) { bean ->
|
marge(Bean4) { bean ->
|
||||||
bean.factoryMethod = "getInstance"
|
bean.factoryMethod = "getInstance"
|
||||||
person = "marge"
|
person = "marge"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -600,28 +600,28 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
assert "mcBain", appCtx.getBean("mcBain").person
|
assert "mcBain", appCtx.getBean("mcBain").person
|
||||||
}
|
}
|
||||||
|
|
||||||
void testGetBeanDefinitions() {
|
void testGetBeanDefinitions() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
jeff(Bean1) {
|
jeff(Bean1) {
|
||||||
person = 'jeff'
|
person = 'jeff'
|
||||||
}
|
}
|
||||||
graeme(Bean1) {
|
graeme(Bean1) {
|
||||||
person = 'graeme'
|
person = 'graeme'
|
||||||
}
|
}
|
||||||
guillaume(Bean1) {
|
guillaume(Bean1) {
|
||||||
person = 'guillaume'
|
person = 'guillaume'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
junit.framework.TestCase.assertEquals 'beanDefinitions was the wrong size', 3, reader.registry.beanDefinitionCount
|
junit.framework.TestCase.assertEquals 'beanDefinitions was the wrong size', 3, reader.registry.beanDefinitionCount
|
||||||
assertNotNull 'beanDefinitions did not contain jeff', reader.registry.getBeanDefinition('jeff')
|
assertNotNull 'beanDefinitions did not contain jeff', reader.registry.getBeanDefinition('jeff')
|
||||||
assertNotNull 'beanDefinitions did not contain guillaume', reader.registry.getBeanDefinition('guillaume')
|
assertNotNull 'beanDefinitions did not contain guillaume', reader.registry.getBeanDefinition('guillaume')
|
||||||
assertNotNull 'beanDefinitions did not contain graeme', reader.registry.getBeanDefinition('graeme')
|
assertNotNull 'beanDefinitions did not contain graeme', reader.registry.getBeanDefinition('graeme')
|
||||||
}
|
}
|
||||||
|
|
||||||
void testBeanWithFactoryBean() {
|
void testBeanWithFactoryBean() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
@@ -679,11 +679,11 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
reader.beans {
|
reader.beans {
|
||||||
quest(HolyGrailQuest)
|
quest(HolyGrailQuest)
|
||||||
|
|
||||||
knight(KnightOfTheRoundTable, "Bedivere") {
|
knight(KnightOfTheRoundTable, "Bedivere") {
|
||||||
quest = ref("quest")
|
quest = ref("quest")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
@@ -692,43 +692,43 @@ class GroovyBeanDefinitionReaderTests extends GroovyTestCase {
|
|||||||
knight.embarkOnQuest()
|
knight.embarkOnQuest()
|
||||||
}
|
}
|
||||||
|
|
||||||
void testAbstractBeanSpecifyingClass() {
|
void testAbstractBeanSpecifyingClass() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
reader.beans {
|
reader.beans {
|
||||||
abstractKnight(KnightOfTheRoundTable) { bean ->
|
abstractKnight(KnightOfTheRoundTable) { bean ->
|
||||||
bean.'abstract' = true
|
bean.'abstract' = true
|
||||||
leader = "King Arthur"
|
leader = "King Arthur"
|
||||||
}
|
}
|
||||||
|
|
||||||
lancelot("lancelot") { bean ->
|
lancelot("lancelot") { bean ->
|
||||||
bean.parent = ref("abstractKnight")
|
bean.parent = ref("abstractKnight")
|
||||||
}
|
}
|
||||||
|
|
||||||
abstractPerson(Bean1) { bean ->
|
abstractPerson(Bean1) { bean ->
|
||||||
bean.'abstract'=true
|
bean.'abstract'=true
|
||||||
age = 45
|
age = 45
|
||||||
}
|
}
|
||||||
homerBean { bean ->
|
homerBean { bean ->
|
||||||
bean.parent = ref("abstractPerson")
|
bean.parent = ref("abstractPerson")
|
||||||
person = "homer"
|
person = "homer"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
|
|
||||||
def lancelot = appCtx.getBean("lancelot")
|
def lancelot = appCtx.getBean("lancelot")
|
||||||
assertEquals "King Arthur", lancelot.leader
|
assertEquals "King Arthur", lancelot.leader
|
||||||
assertEquals "lancelot", lancelot.name
|
assertEquals "lancelot", lancelot.name
|
||||||
|
|
||||||
def homerBean = appCtx.getBean("homerBean")
|
def homerBean = appCtx.getBean("homerBean")
|
||||||
|
|
||||||
assertEquals 45, homerBean.age
|
assertEquals 45, homerBean.age
|
||||||
assertEquals "homer", homerBean.person
|
assertEquals "homer", homerBean.person
|
||||||
}
|
}
|
||||||
|
|
||||||
void testGroovyBeanDefinitionReaderWithScript() {
|
void testGroovyBeanDefinitionReaderWithScript() {
|
||||||
def script = '''
|
def script = '''
|
||||||
def appCtx = new org.springframework.context.support.GenericGroovyApplicationContext()
|
def appCtx = new org.springframework.context.support.GenericGroovyApplicationContext()
|
||||||
appCtx.reader.beans {
|
appCtx.reader.beans {
|
||||||
quest(org.springframework.context.groovy.HolyGrailQuest) {}
|
quest(org.springframework.context.groovy.HolyGrailQuest) {}
|
||||||
@@ -737,38 +737,38 @@ knight(org.springframework.context.groovy.KnightOfTheRoundTable, "Bedivere") { q
|
|||||||
}
|
}
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
return appCtx
|
return appCtx
|
||||||
'''
|
'''
|
||||||
def appCtx = new GroovyShell().evaluate(script)
|
def appCtx = new GroovyShell().evaluate(script)
|
||||||
|
|
||||||
def knight = appCtx.getBean('knight')
|
def knight = appCtx.getBean('knight')
|
||||||
knight.embarkOnQuest()
|
knight.embarkOnQuest()
|
||||||
}
|
}
|
||||||
|
|
||||||
// test for GRAILS-5057
|
// test for GRAILS-5057
|
||||||
void testRegisterBeans() {
|
void testRegisterBeans() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
|
|
||||||
reader.beans {
|
reader.beans {
|
||||||
personA(AdvisedPerson) {
|
personA(AdvisedPerson) {
|
||||||
name = "Bob"
|
name = "Bob"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
assertEquals "Bob", appCtx.getBean("personA").name
|
assertEquals "Bob", appCtx.getBean("personA").name
|
||||||
|
|
||||||
appCtx = new GenericApplicationContext()
|
appCtx = new GenericApplicationContext()
|
||||||
reader = new GroovyBeanDefinitionReader(appCtx)
|
reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
personA(AdvisedPerson) {
|
personA(AdvisedPerson) {
|
||||||
name = "Fred"
|
name = "Fred"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
assertEquals "Fred", appCtx.getBean("personA").name
|
assertEquals "Fred", appCtx.getBean("personA").name
|
||||||
}
|
}
|
||||||
|
|
||||||
void testListOfBeansAsConstructorArg() {
|
void testListOfBeansAsConstructorArg() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
@@ -815,80 +815,80 @@ return appCtx
|
|||||||
assertEquals "bart", beanWithMap.peopleByName.bart.person
|
assertEquals "bart", beanWithMap.peopleByName.bart.person
|
||||||
}
|
}
|
||||||
|
|
||||||
void testAnonymousInnerBeanViaBeanMethod() {
|
void testAnonymousInnerBeanViaBeanMethod() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
bart(Bean1) {
|
bart(Bean1) {
|
||||||
person = "bart"
|
person = "bart"
|
||||||
age = 11
|
age = 11
|
||||||
}
|
}
|
||||||
lisa(Bean1) {
|
lisa(Bean1) {
|
||||||
person = "lisa"
|
person = "lisa"
|
||||||
age = 9
|
age = 9
|
||||||
}
|
}
|
||||||
marge(Bean2) {
|
marge(Bean2) {
|
||||||
person = "marge"
|
person = "marge"
|
||||||
bean1 = bean(Bean1) {
|
bean1 = bean(Bean1) {
|
||||||
person = "homer"
|
person = "homer"
|
||||||
age = 45
|
age = 45
|
||||||
props = [overweight:true, height:"1.8m"]
|
props = [overweight:true, height:"1.8m"]
|
||||||
children = ["bart", "lisa"]
|
children = ["bart", "lisa"]
|
||||||
}
|
}
|
||||||
children = [bart, lisa]
|
children = [bart, lisa]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
|
|
||||||
def marge = appCtx.getBean("marge")
|
def marge = appCtx.getBean("marge")
|
||||||
assertEquals "homer", marge.bean1.person
|
assertEquals "homer", marge.bean1.person
|
||||||
}
|
}
|
||||||
|
|
||||||
void testAnonymousInnerBeanViaBeanMethodWithConstructorArgs() {
|
void testAnonymousInnerBeanViaBeanMethodWithConstructorArgs() {
|
||||||
def appCtx = new GenericApplicationContext()
|
def appCtx = new GenericApplicationContext()
|
||||||
def reader = new GroovyBeanDefinitionReader(appCtx)
|
def reader = new GroovyBeanDefinitionReader(appCtx)
|
||||||
reader.beans {
|
reader.beans {
|
||||||
bart(Bean1) {
|
bart(Bean1) {
|
||||||
person = "bart"
|
person = "bart"
|
||||||
age = 11
|
age = 11
|
||||||
}
|
}
|
||||||
lisa(Bean1) {
|
lisa(Bean1) {
|
||||||
person = "lisa"
|
person = "lisa"
|
||||||
age = 9
|
age = 9
|
||||||
}
|
}
|
||||||
marge(Bean2) {
|
marge(Bean2) {
|
||||||
person = "marge"
|
person = "marge"
|
||||||
bean3 = bean(Bean3, "homer", lisa) {
|
bean3 = bean(Bean3, "homer", lisa) {
|
||||||
person = "homer"
|
person = "homer"
|
||||||
age = 45
|
age = 45
|
||||||
}
|
}
|
||||||
children = [bart, lisa]
|
children = [bart, lisa]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
appCtx.refresh()
|
appCtx.refresh()
|
||||||
|
|
||||||
def marge = appCtx.getBean("marge")
|
def marge = appCtx.getBean("marge")
|
||||||
|
|
||||||
assertEquals "homer", marge.bean3.person
|
assertEquals "homer", marge.bean3.person
|
||||||
assertEquals "lisa", marge.bean3.bean1.person
|
assertEquals "lisa", marge.bean3.bean1.person
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class HolyGrailQuest {
|
class HolyGrailQuest {
|
||||||
void start() { println "lets begin" }
|
void start() { println "lets begin" }
|
||||||
}
|
}
|
||||||
class KnightOfTheRoundTable {
|
class KnightOfTheRoundTable {
|
||||||
String name
|
String name
|
||||||
String leader
|
String leader
|
||||||
KnightOfTheRoundTable(String n) {
|
KnightOfTheRoundTable(String n) {
|
||||||
this.name = n
|
this.name = n
|
||||||
}
|
}
|
||||||
HolyGrailQuest quest
|
HolyGrailQuest quest
|
||||||
|
|
||||||
void embarkOnQuest() {
|
void embarkOnQuest() {
|
||||||
quest.start()
|
quest.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// simple bean
|
// simple bean
|
||||||
@@ -900,7 +900,7 @@ class Bean1 {
|
|||||||
}
|
}
|
||||||
// bean referencing other bean
|
// bean referencing other bean
|
||||||
class Bean2 {
|
class Bean2 {
|
||||||
int age
|
int age
|
||||||
String person
|
String person
|
||||||
Bean1 bean1
|
Bean1 bean1
|
||||||
Bean3 bean3
|
Bean3 bean3
|
||||||
@@ -928,17 +928,17 @@ class Bean4 {
|
|||||||
}
|
}
|
||||||
// bean with List-valued constructor arg
|
// bean with List-valued constructor arg
|
||||||
class Bean5 {
|
class Bean5 {
|
||||||
Bean5(List<Bean1> people) {
|
Bean5(List<Bean1> people) {
|
||||||
this.people = people
|
this.people = people
|
||||||
}
|
}
|
||||||
List<Bean1> people
|
List<Bean1> people
|
||||||
}
|
}
|
||||||
// bean with Map-valued constructor arg
|
// bean with Map-valued constructor arg
|
||||||
class Bean6 {
|
class Bean6 {
|
||||||
Bean6(Map<String, Bean1> peopleByName) {
|
Bean6(Map<String, Bean1> peopleByName) {
|
||||||
this.peopleByName = peopleByName
|
this.peopleByName = peopleByName
|
||||||
}
|
}
|
||||||
Map<String, Bean1> peopleByName
|
Map<String, Bean1> peopleByName
|
||||||
}
|
}
|
||||||
// a factory bean
|
// a factory bean
|
||||||
class Bean1Factory {
|
class Bean1Factory {
|
||||||
@@ -949,44 +949,44 @@ class Bean1Factory {
|
|||||||
class ScopeTest {}
|
class ScopeTest {}
|
||||||
class TestScope implements Scope {
|
class TestScope implements Scope {
|
||||||
|
|
||||||
int instanceCount
|
int instanceCount
|
||||||
|
|
||||||
|
|
||||||
public Object remove(String name) {
|
public Object remove(String name) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerDestructionCallback(String name, Runnable callback) {
|
public void registerDestructionCallback(String name, Runnable callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getConversationId() {
|
public String getConversationId() {
|
||||||
return "mock"
|
return "mock"
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object get(String name, ObjectFactory<?> objectFactory) {
|
public Object get(String name, ObjectFactory<?> objectFactory) {
|
||||||
instanceCount++
|
instanceCount++
|
||||||
objectFactory.getObject()
|
objectFactory.getObject()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object resolveContextualObject(String s) {
|
public Object resolveContextualObject(String s) {
|
||||||
return null; // noop
|
return null; // noop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class BirthdayCardSender {
|
class BirthdayCardSender {
|
||||||
List peopleSentCards = []
|
List peopleSentCards = []
|
||||||
public void onBirthday(AdvisedPerson person) {
|
public void onBirthday(AdvisedPerson person) {
|
||||||
peopleSentCards << person
|
peopleSentCards << person
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Component(value = "person")
|
@Component(value = "person")
|
||||||
public class AdvisedPerson {
|
public class AdvisedPerson {
|
||||||
int age;
|
int age;
|
||||||
String name;
|
String name;
|
||||||
|
|
||||||
public void birthday() {
|
public void birthday() {
|
||||||
++age;
|
++age;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SomeClass {
|
class SomeClass {
|
||||||
|
|||||||
@@ -35,32 +35,32 @@ public class GroovyApplicationContextTests extends TestCase {
|
|||||||
assertEquals("Grails", framework);
|
assertEquals("Grails", framework);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLoadingMultipleConfigFiles() {
|
public void testLoadingMultipleConfigFiles() {
|
||||||
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
|
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(
|
||||||
"org/springframework/context/groovy/applicationContext2.groovy",
|
"org/springframework/context/groovy/applicationContext2.groovy",
|
||||||
"org/springframework/context/groovy/applicationContext.groovy");
|
"org/springframework/context/groovy/applicationContext.groovy");
|
||||||
|
|
||||||
Object framework = ctx.getBean("framework");
|
Object framework = ctx.getBean("framework");
|
||||||
assertNotNull("could not find framework bean", framework);
|
assertNotNull("could not find framework bean", framework);
|
||||||
assertEquals("Grails", framework);
|
assertEquals("Grails", framework);
|
||||||
|
|
||||||
Object company = ctx.getBean("company");
|
Object company = ctx.getBean("company");
|
||||||
assertNotNull("could not find company bean", company);
|
assertNotNull("could not find company bean", company);
|
||||||
assertEquals("SpringSource", company);
|
assertEquals("SpringSource", company);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLoadingMultipleConfigFilesWithRelativeClass() {
|
public void testLoadingMultipleConfigFilesWithRelativeClass() {
|
||||||
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
|
GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext();
|
||||||
ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy");
|
ctx.load(GroovyApplicationContextTests.class, "applicationContext2.groovy", "applicationContext.groovy");
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
Object framework = ctx.getBean("framework");
|
Object framework = ctx.getBean("framework");
|
||||||
assertNotNull("could not find framework bean", framework);
|
assertNotNull("could not find framework bean", framework);
|
||||||
assertEquals("Grails", framework);
|
assertEquals("Grails", framework);
|
||||||
|
|
||||||
Object company = ctx.getBean("company");
|
Object company = ctx.getBean("company");
|
||||||
assertNotNull("could not find company bean", company);
|
assertNotNull("could not find company bean", company);
|
||||||
assertEquals("SpringSource", company);
|
assertEquals("SpringSource", company);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user