2 Jul 2005 17:11
adding dependency injection to groovy (was Re: [groovy-user] grails: Groovy class loader and Spring)
Just a quick thought I've had on and off over the last year. It might
be interesting, in Groovy, to provide a Dependency Injection hook
into Groovy. e.g. a Groovy class is associated with a
GroovyClassLoader, which could have some kinda Injector plugin, which
could be implemented in, say, Spring to do dependency injection using
a normal script.
e.g. imagine code like this
interface Cheese {
void foo()
}
then in some script you just did
// lets create an instance of an interface...
def f = new Cheese()
which is pretty wacky - but what that'd do is try and ask the
Injector to make an instance of Cheese - kinda like Spring's
BeanContext.getBeanForType(Class) and the similar operation in Pico.
We could use the rule, whenever you use
new InterfaceNameOrAbstractClass()
then we'd use the Injector to make an instance. The really neat thing
is, this means we get all the benefits of dependency injection (i.e.
our code gets injected with the correctly configured specific
implementation we need) yet rather than constructor or property based
injection - this is Groovy injection :) i.e. we can inject into any
groovy expression using 'new'. Kinda neat eh?
One thing the Spring/Pico folks have found very useful, is injecting
via name. Especially when things like database connections etc. So we
could allow a String parameter.
// connection is an interface
// so lets use dependency injection
def conn = new Connection("orderDb")
which if we were using Spring as the injector would be the equivalent
of...
def conn = (Connection) beanFactory.getBean("orderDb")
So we could fairly easily provide access to dependency injection; by
type/interface or by name (and type check too) - with clean
integration to the existing DI tools like Spring but also allowing
other solutions to be explored. e.g. we could use some groovy script
instead of XML to define the injected objects...
userName = "James"
password="cheese"
orderDb = {
createDatabase("jdbc://....", userName, passsword)
}
BTW I've written this email after taking a red eye back to the UK, so
please excuse me if its a bit wacky - I've not had much coffee yet :).
Thoughts?
On 29 Jun 2005, at 04:57, Steven Devijver wrote:
> Following the thread on Grails I think it makes sense to split the
> discussion into different topics as the entire Grails project is
> pretty big.
>
> Grails faces two challenges when it comes to POGO class loading and
> object instantiation in the Spring application context.
>
> The first challenge is to have Hibernate create the POGO's through
> reflection. I have walked down this road before with Janino. Basically
> we need a Groovy class loader configured in Spring that exposes a
> class loader that has loaded the persistent POGO's configured in
> Hibernate. This is probably a FactoryBean. Next we need an AOP method
> interceptor to replace the thread context loader with the Groovy class
> loader and back again to make the POGO classes available to Hibernate
> (Hibernate uses the thread context loader).
>
> The second challenge is to create MVC and business logic POGO
> instances in the application context. For this we can use the Groovy
> support currently living in the Spring sandbox.
>
> So we should probably start some work on these two topics. Any
> volunteers?
>
> Any thoughts or comments on this?
>
> Steven
>
> --
> "If you want to be a different fish, you gotta jump out of the
> school."
> -- Captain Beefheart
>
James
-------
http://radio.weblogs.com/0112098/
RSS Feed