Skip to content

Commit 64fd0b0

Browse files
author
David Syer
committed
SPR-6268: Add proxy-target-class to <lang:groovy/>
1 parent 5bfeb34 commit 64fd0b0

File tree

11 files changed

+305
-120
lines changed

11 files changed

+305
-120
lines changed

org.springframework.context/src/main/java/org/springframework/scripting/config/ScriptBeanDefinitionParser.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ class ScriptBeanDefinitionParser extends AbstractBeanDefinitionParser {
7070
private static final String SCRIPT_INTERFACES_ATTRIBUTE = "script-interfaces";
7171

7272
private static final String REFRESH_CHECK_DELAY_ATTRIBUTE = "refresh-check-delay";
73-
74-
private static final String CUSTOMIZER_REF_ATTRIBUTE = "customizer-ref";
7573

74+
private static final String PROXY_TARGET_CLASS_ATTRIBUTE = "proxy-target-class";
75+
76+
private static final String CUSTOMIZER_REF_ATTRIBUTE = "customizer-ref";
7677

7778
/**
7879
* The {@link org.springframework.scripting.ScriptFactory} class that this
7980
* parser instance will create bean definitions for.
8081
*/
8182
private final String scriptFactoryClassName;
8283

83-
8484
/**
8585
* Create a new instance of this parser, creating bean definitions for the
8686
* supplied {@link org.springframework.scripting.ScriptFactory} class.
@@ -90,7 +90,6 @@ public ScriptBeanDefinitionParser(String scriptFactoryClassName) {
9090
this.scriptFactoryClassName = scriptFactoryClassName;
9191
}
9292

93-
9493
/**
9594
* Parses the dynamic object element and returns the resulting bean definition.
9695
* Registers a {@link ScriptFactoryPostProcessor} if needed.
@@ -110,7 +109,8 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
110109
GenericBeanDefinition bd = new GenericBeanDefinition();
111110
bd.setBeanClassName(this.scriptFactoryClassName);
112111
bd.setSource(parserContext.extractSource(element));
113-
112+
bd.setAttribute(ScriptFactoryPostProcessor.LANGUAGE_ATTRIBUTE, element.getLocalName());
113+
114114
// Determine bean scope.
115115
String scope = element.getAttribute(SCOPE_ATTRIBUTE);
116116
if (StringUtils.hasLength(scope)) {
@@ -123,8 +123,7 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
123123
// Only "byType" and "byName" supported, but maybe other default inherited...
124124
if (autowireMode == GenericBeanDefinition.AUTOWIRE_AUTODETECT) {
125125
autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE;
126-
}
127-
else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
126+
} else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
128127
autowireMode = GenericBeanDefinition.AUTOWIRE_NO;
129128
}
130129
bd.setAutowireMode(autowireMode);
@@ -134,31 +133,34 @@ else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
134133
bd.setDependencyCheck(parserContext.getDelegate().getDependencyCheck(dependencyCheck));
135134

136135
// Retrieve the defaults for bean definitions within this parser context
137-
BeanDefinitionDefaults beanDefinitionDefaults =
138-
parserContext.getDelegate().getBeanDefinitionDefaults();
136+
BeanDefinitionDefaults beanDefinitionDefaults = parserContext.getDelegate().getBeanDefinitionDefaults();
139137

140138
// Determine init method and destroy method.
141139
String initMethod = element.getAttribute(INIT_METHOD_ATTRIBUTE);
142140
if (StringUtils.hasLength(initMethod)) {
143141
bd.setInitMethodName(initMethod);
144-
}
145-
else if (beanDefinitionDefaults.getInitMethodName() != null) {
142+
} else if (beanDefinitionDefaults.getInitMethodName() != null) {
146143
bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName());
147144
}
148145

149146
String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
150147
if (StringUtils.hasLength(destroyMethod)) {
151148
bd.setDestroyMethodName(destroyMethod);
152-
}
153-
else if (beanDefinitionDefaults.getDestroyMethodName() != null) {
149+
} else if (beanDefinitionDefaults.getDestroyMethodName() != null) {
154150
bd.setDestroyMethodName(beanDefinitionDefaults.getDestroyMethodName());
155151
}
156152

157153
// Attach any refresh metadata.
158154
String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
159155
if (StringUtils.hasText(refreshCheckDelay)) {
160-
bd.setAttribute(
161-
ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, new Long(refreshCheckDelay));
156+
bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, new Long(refreshCheckDelay));
157+
}
158+
159+
// Attach any proxy target class metadata.
160+
String proxyTargetClass = element.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
161+
if (StringUtils.hasText(proxyTargetClass)) {
162+
Boolean flag = new Boolean(proxyTargetClass);
163+
bd.setAttribute(ScriptFactoryPostProcessor.PROXY_TARGET_CLASS_ATTRIBUTE, flag);
162164
}
163165

164166
// Add constructor arguments.
@@ -168,14 +170,13 @@ else if (beanDefinitionDefaults.getDestroyMethodName() != null) {
168170
if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) {
169171
cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE));
170172
}
171-
173+
172174
// This is used for Groovy. It's a bean reference to a customizer bean.
173175
if (element.hasAttribute(CUSTOMIZER_REF_ATTRIBUTE)) {
174176
String customizerBeanName = element.getAttribute(CUSTOMIZER_REF_ATTRIBUTE);
175177
if (!StringUtils.hasText(customizerBeanName)) {
176178
parserContext.getReaderContext().error("Attribute 'customizer-ref' has empty value", element);
177-
}
178-
else {
179+
} else {
179180
cav.addIndexedArgumentValue(constructorArgNum++, new RuntimeBeanReference(customizerBeanName));
180181
}
181182
}
@@ -197,15 +198,12 @@ private String resolveScriptSource(Element element, XmlReaderContext readerConte
197198
if (hasScriptSource && !elements.isEmpty()) {
198199
readerContext.error("Only one of 'script-source' and 'inline-script' should be specified.", element);
199200
return null;
200-
}
201-
else if (hasScriptSource) {
201+
} else if (hasScriptSource) {
202202
return element.getAttribute(SCRIPT_SOURCE_ATTRIBUTE);
203-
}
204-
else if (!elements.isEmpty()) {
203+
} else if (!elements.isEmpty()) {
205204
Element inlineElement = (Element) elements.get(0);
206205
return "inline:" + DomUtils.getTextValue(inlineElement);
207-
}
208-
else {
206+
} else {
209207
readerContext.error("Must specify either 'script-source' or 'inline-script'.", element);
210208
return null;
211209
}

org.springframework.context/src/main/java/org/springframework/scripting/config/ScriptingDefaultsParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.w3c.dom.Element;
2020

2121
import org.springframework.beans.factory.config.BeanDefinition;
22+
import org.springframework.beans.factory.config.TypedStringValue;
2223
import org.springframework.beans.factory.xml.BeanDefinitionParser;
2324
import org.springframework.beans.factory.xml.ParserContext;
2425
import org.springframework.util.StringUtils;
@@ -31,6 +32,8 @@ public class ScriptingDefaultsParser implements BeanDefinitionParser {
3132

3233
private static final String REFRESH_CHECK_DELAY_ATTRIBUTE = "refresh-check-delay";
3334

35+
private static final String PROXY_TARGET_CLASS_ATTRIBUTE = "proxy-target-class";
36+
3437

3538
public BeanDefinition parse(Element element, ParserContext parserContext) {
3639
BeanDefinition bd =
@@ -39,6 +42,10 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
3942
if (StringUtils.hasText(refreshCheckDelay)) {
4043
bd.getPropertyValues().add("defaultRefreshCheckDelay", new Long(refreshCheckDelay));
4144
}
45+
String proxyTargetClass = element.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
46+
if (StringUtils.hasText(proxyTargetClass)) {
47+
bd.getPropertyValues().add("defaultProxyTargetClass", new TypedStringValue(proxyTargetClass, Boolean.class));
48+
}
4249
return null;
4350
}
4451

0 commit comments

Comments
 (0)