Issue Details (XML | Word | Printable)

Key: SPR-3161
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Juergen Hoeller
Reporter: chris tam
Votes: 2
Watchers: 4
Operations

If you were logged in you would be able to see more operations.
Spring Framework

Add scope attribute to script tags in lang namespace

Created: 14/Feb/07 02:33 AM   Updated: 15/Mar/07 07:54 AM   Resolved: 14/Mar/07 11:22 AM
Component/s: SpringCORE
Affects Version/s: 2.0.3
Fix Version/s: 2.0.4

Time Tracking:
Not Specified

File Attachments: 1. Text File ScriptScope.patch (3 kB)

Environment: Win2000

Virtual Machine: Sun JVM - 1.4.2
Platform: Standalone


 Description  « Hide

In spring framework 2.0.3, all dynamic language scripting bean scope is restricted to "singleton". For example,
...
<lang:groovy id="GroovyPerson" script-source="classpath: org/test/GroovyPerson.groovy">
<lang:property name="name" value="ruby"/>
</lang:groovy>
...
The above "GroovyPerson" bean scope is always singleton which will restrict the usage of the bean. For example:
...
Person person1 = (Person)context.getBean("GroovyPerson");
person1.setName("chris tam");
Person person2 = (Person)context.getBean("GroovyPerson");
person2.setName("Rod Johnson");
// should print out "chris tam" but print out "Rod Johnson"
System.out.println(person1.getName());
...
I have checked the spring-lang-2.0.xsd and ScriptBeanDefinitionParser files and do some experiments on adding an "script-scope" attribute to the lang namespace to control the scripting bean scope and so far it works fine. Is it possible to add an "script-scope" attribute to the scripting bean? Thanks a lot for any help

Thanks and best regards
chris tam
xenium



Mark Menard added a comment - 22/Feb/07 09:54 AM

I've spent the last several days digging into this. If I got some guidance I think I might be able to help with this issue.

I'm interested, because I'm trying to move my actions for Struts 2 to Groovy. They need to have prototype scope to make it work though.

Thanks,

Mark


chris tam added a comment - 22/Feb/07 12:29 PM

I will prepare a zip file which contains my workaround for this scope problem and upload here. In order to allow script bean has scope, you need to change the following file:
org.springframework.scripting.config.ScriptBeanDefinitionParser.java

In the ScriptBeanDefinitionParser java file, there is a method called parseInternal where you should find a RootBeanDefinition object which is used to control the scope of the script bean.
The current code of Spring 2.0.3 is:
,,,
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
...
RootBeanDefinition beanDefinition =
new RootBeanDefinition(this.scriptFactoryClass);
beanDefinition.setSource(parserContext.extractSource(element));
...

In order to control the script bean scope, the above code should modify to:
,,,
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
...
RootBeanDefinition beanDefinition =
new RootBeanDefinition(this.scriptFactoryClass);
beanDefinition.setSource(parserContext.extractSource(element));

// suppose you have already get the script scope value in above and
// store in a String variable called scriptScope. scriptScope should be
// either singleton or prototype
beanDefinition.setScope(scriptScope) ;
...
You can refer to the parseInternal method to get an idea how all the attribute values are retrieved. Also you need to edit the xsd file to allow script-scope attribute. The xsd file is:
src/org/springframework/scripting/config/spring-lang-2.0.xsd

Find the following section in spring-lang-2.0.xsd:
...
<xsd:complexType name="simpleScriptType">
...
<xsd:attribute name="script-source" type="xsd:string">
...
</xsd:attribute>
...

Then add a new xsd attribute script-scope after the above script-source attribute. For example:

...
<xsd:complexType name="simpleScriptType">
...
<xsd:attribute name="script-source" type="xsd:string">
...
</xsd:attribute>
<xsd:attribute name="script-scope" type="xsd:string">
</xsd:attribute>
...

Rebuild the spring.jar and with the update xsd file. You can add the "script-scope" attribute to groovy bean definition like normal bean. If you found any problem, please alert me. So far it works fine in my application. But I am still afraid of any hidden problem.

cheers
chris tam
xenium


Mark Menard added a comment - 23/Feb/07 05:56 PM

This is a quick patch against 2.0.2 to enable 'prototype' and 'singleton' script beans. It adds 'script-scope' to the lang:script_lang tag. The acceptable values are 'prototype' and 'singleton'. If no script-scope is present beans are singleton.

I have not tested this extensively. I also have no experience with the Spring tests. If someone could provide some advice I'll write some tests.

This patch would probably also work against HEAD, but I had some issues with getting that working, and had the 2.0.2 source.


chris tam added a comment - 23/Feb/07 11:56 PM

Dear Mark

Thanks a lot. I will try the patch. Since I am currently working on a project of my company, I will try your patch as soon as possible and give any feedback.

cheers
chris tam
xenium


chris tam added a comment - 24/Feb/07 04:16 AM

Dear Mark

Thanks a lot for your patch. I have ported my junit test programs for 2.0.3 to 2.0.2 and your patch passes all junit test programs for Groovy. Since there are some problems for Beanshell and JRuby in 2.0.2, I have skipped the test for Beanshell and JRuby. You have mentioned a "HEAD" problem in your comment. Would you mind tell me what is "HEAD" problem? I have 2 concerns about the solution:
1. Is there any hidden problem with the refresh attribute?
2. Is there any hidden problem with inline-script?
If you find any problem, please alert me. Since my junit programs contains some codes which are related to my current project of my company. I need to clean it up before I attach my junit test program here. I must thank again for your help.

cheers
chris tam
xenium


Juergen Hoeller added a comment - 14/Mar/07 11:22 AM

Thanks for pointing this out! While we do support custom script scopes at the ScriptFactory level already (as of 2.0.3), this is not exposed in the "lang" namespace yet.

Hence I've added a "scope" attribute to all script tags in the "lang" namespace, allowing for non-singleton script objects defined in that convenient style, analogous to the "scope" attribute in a standard bean definition.

This should be available in tonight's 2.0.4 snapshot. Please give it a try and let me know whether it works for you!

Juergen


chris tam added a comment - 15/Mar/07 07:54 AM

The "scope" attribute in 2.0.4 snapshot works fine in my application and junit test programs. Thanks Juergen.

cheers
chris tam
xenium