You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Larry Singer <la...@gmail.com> on 2004/09/22 04:38:12 UTC
Using Spring to inject dependencies
Hi All,
I have just written a little class that allows us to inject
dependencies using Spring into a Tapestry page (or any other bean).
This has the advantage of putting back onto Spring most of the
responsibility for ensuring that dependencies are there. (I will be
submitting this to the Spring mailing list also.)
First the class DependencyInjector that does the actual work simply by
calling the appropriate Spring routine:
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.AbstractApplicationContext;
/**
* Inject dependancies specified in the Spring definitions into an
existing bean.
* This only injects parameter dependencies.
* It cannot inject constructor dependencies since the object has
already been created.
*
* @author Larry Singer 22-Sep-2004
*/
public class DependencyInjector extends DefaultListableBeanFactory
{
public DependencyInjector(AbstractApplicationContext applicationContext) {
super(applicationContext.getBeanFactory());
}
public void injectDependancies(String beanName, Object bean)
{
RootBeanDefinition mergedBeanDefinition =
getMergedBeanDefinition(beanName, true);
BeanWrapper bw = new BeanWrapperImpl(bean);
populateBean(beanName, mergedBeanDefinition, bw);
}
}
To use this in a Tapestry page I call this object from the attach
method of the page:
public void attach(IEngine engine)
{
super.attach(engine);
DependencyInjector tpi = new
DependencyInjector((XmlWebApplicationContext)
getApplicationContext());
tpi.injectDependancies(getPageName(), this);
}
This is placed in a super class of all the pages so that it is
automatically called.
Disclaimer: This is intended as a proof of concept. There may be
better ways to do this in Spring. Also it has not yet been thoroughly
tested.
Larry Singer
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Using Spring to inject dependencies
Posted by Larry Singer <la...@gmail.com>.
On Wed, 22 Sep 2004 08:45:47 -0700, Nicholas Lesiecki
<nl...@vmsinfo.com> wrote:
> Larry,
>
> I had the same idea. Take a look at a new bit of API that the Spring team
> added to 1.1:
>
> http://opensource.atlassian.com/projects/spring/browse/SPR-266
Thanks. I didn't find that method. I don't need my little class any
more. All I need to do is get the bean factory and call this method.
public void attach(IEngine engine)
{
super.attach(engine);
// inject the dependencies for the page
XmlWebApplicationContext ac = (XmlWebApplicationContext)
getApplicationContext();
ac.getBeanFactory().applyBeanPropertyValues(this, getPageName());
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: Using Spring to inject dependencies
Posted by Nicholas Lesiecki <nl...@vmsinfo.com>.
Larry,
I had the same idea. Take a look at a new bit of API that the Spring team
added to 1.1:
http://opensource.atlassian.com/projects/spring/browse/SPR-266
Relevant bit:
<<<
I've just added an applyBeanPropertyValues(Object existingBean, String name)
method to the AutowireCapableBeanFactory interface, analogous to the
existing autowireBeanProperties method. It simply applies the property
values from the bean definition with the given name to an existing bean
instance, which should work nicely for Tapestry Page objects.
>>>
Meaning you can avoid:
<<<
BeanWrapper bw = new BeanWrapperImpl(bean);
>>>
Cheers,
Nick
-----Original Message-----
From: Larry Singer [mailto:larry.singer@gmail.com]
Sent: Tuesday, September 21, 2004 7:38 PM
To: tapestry-user@jakarta.apache.org
Subject: Using Spring to inject dependencies
Hi All,
I have just written a little class that allows us to inject
dependencies using Spring into a Tapestry page (or any other bean).
This has the advantage of putting back onto Spring most of the
responsibility for ensuring that dependencies are there. (I will be
submitting this to the Spring mailing list also.)
First the class DependencyInjector that does the actual work simply by
calling the appropriate Spring routine:
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.AbstractApplicationContext;
/**
* Inject dependancies specified in the Spring definitions into an
existing bean.
* This only injects parameter dependencies.
* It cannot inject constructor dependencies since the object has
already been created.
*
* @author Larry Singer 22-Sep-2004
*/
public class DependencyInjector extends DefaultListableBeanFactory
{
public DependencyInjector(AbstractApplicationContext
applicationContext) {
super(applicationContext.getBeanFactory());
}
public void injectDependancies(String beanName, Object bean)
{
RootBeanDefinition mergedBeanDefinition =
getMergedBeanDefinition(beanName, true);
BeanWrapper bw = new BeanWrapperImpl(bean);
populateBean(beanName, mergedBeanDefinition, bw);
}
}
To use this in a Tapestry page I call this object from the attach
method of the page:
public void attach(IEngine engine)
{
super.attach(engine);
DependencyInjector tpi = new
DependencyInjector((XmlWebApplicationContext)
getApplicationContext());
tpi.injectDependancies(getPageName(), this);
}
This is placed in a super class of all the pages so that it is
automatically called.
Disclaimer: This is intended as a proof of concept. There may be
better ways to do this in Spring. Also it has not yet been thoroughly
tested.
Larry Singer
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org