PDA

View Full Version : bean definition using inline scripting languages?


jbetancourt
Aug 28th, 2004, 04:28 PM
Probably a bad idea (or again its in Spring already!) but ....

For diagnostics I created a simple bean that announced its construction
and property setting very loudly ( a banner in the log file).

But, despite the other methods of accomplishing this, like tailing the log
file and redirecting to a grep or using a debugger, this leads to:
why even have to create these temporary or minor classes?

What if a bean definition could be implemented inline right in the bean
descriptor? Sounds horrible, I know. But this is not like Jelly, an XML language, rather its
just a conveniant well-known approach (very easy to misuse too). Ant and Maven, for example,
already allow scripting.

This could be used with AOP, inserting advise via scripting.

The above creation of a throwaway POJO would be a no brainer. The script
could be in any script language but the default is a scripted Java syntax
(there are many Java script compilers such as BeanShell or Janino).
See http://www.janino.net and http://www.beanshell.org

Thus a possible bean definition could be:

<bean id="shouter">
<property name="msg"><value>Hello world!</value></property>

<package>com.........util</package> <!-- optional? -->

<script language="beanshell"> <!-- or jython,Groovy,Janino,Rhino -->
<![CDATA[
public class Shout {
protected String msg;
// stuff snipped .....
}
]]>
</script>
</bean>


-- J. Betancourt

irbouho
Aug 28th, 2004, 11:14 PM
Spring already (in sandbox) supports using beans created with Groovy. You can use Spring intergaration for Groovy as it is, or make some changes to support inline scripts.
More scripting languages are to be added Jython bean factory (http://forum.springframework.org/showthread.php?t=9898&highlight=groovy)

jbetancourt
Aug 29th, 2004, 05:02 AM
Yes, I saw that there is now support for scripting in Spring,
in package org.springframework.beans.factory.script. I guess this is the SPI? I wonder if BSF is applicable here to support any language. See
http://jakarta.apache.org/bsf/

If I have time I will see how to add a beanShell or Janino implementation also.

I also mentioned the idea of inline scripting to see if there are any real strong reasons not to have such a capability. But, I guess if someone needs it, why not.


Spring already (in sandbox) supports using beans created with Groovy. You can use Spring intergaration for Groovy as it is, or make some changes to support inline scripts.
More scripting languages are to be added Jython bean factory (http://forum.springframework.org/viewtopic.php?t=354&highlight=groovy)

jbetancourt
Sep 27th, 2004, 08:15 AM
fyi: The JSR 223: Scripting Pages in JavaTM Web Applications

Is now in early draft review. The title is a little misleading, it started out that way, but now seems to cover scripting in Java in general.

Link: http://jcp.org/en/jsr/detail?id=223

Rod Johnson
Oct 20th, 2004, 07:09 AM
Have you checked out the current scripting support in the sandbox. Supports Groovy and Beanshell and it would be easy to support other languages.

I've looked at BSF but I'm not convinced in adds that much value here. There is also a pretty clear way to support arbitrary languages in Spring's support, more customized for the needs of DI. (Scripts configured via DI and other objects depending on them.)