Combined constructor and setter injection

View: New views
2 Messages — Rating Filter:   Alert me  

Combined constructor and setter injection

by Leos Bitto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I have an application which requires an IoC container to be run. Currently it works with Spring Framework and I would like to try PicoContainer, too. The application is designed to use both constructor and setter injection. The constructor injection is used mainly for mandatory dependencies, while the setters are used maily for optional dependencies - the compoments usually can work without those optional dependencies, for example by having a default value assigned to the appropriate fields directly in the source code. I know that it could be rewritten to use only the constructors, but then the configuration would get a bit messy, because the constructor parameters do not have names (unlike the setters). Additionally, generating tons of constructors for various combinations of optional dependencies is not nice.

The requirement is to have the configuration in an XML file, therefore I have tried to use PicoContainer Script 2.0.1 this way:

picoContainer = new DefaultPicoContainer(new CompositeInjection(new ConstructorInjection(), new SetterInjection()));
final ClassLoader cL = Thread.currentThread().getContextClassLoader();
new XMLContainerBuilder(cL.getResource(config),cL).populateContainer(picoContainer);

However, I am not able to rewrite the Spring XML file to the PicoContainer Script XML file and I start getting the feeling that such mixed dependency injection (constructor + optional setters) is not supportted by PicoContainer. Could someone please prove me wrong?


Leos Bitto

RE: Combined constructor and setter injection

by Michael Rimov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I have an application which requires an IoC container to be run. Currently
> it works with Spring Framework and I would like to try PicoContainer, too.
> The application is designed to use both constructor and setter injection.
> The constructor injection is used mainly for mandatory dependencies, while
> the setters are used maily for optional dependencies - the compoments
> usually can work without those optional dependencies, for example by having
> a default value assigned to the appropriate fields directly in the source
> code. I know that it could be rewritten to use only the constructors, but
> then the configuration would get a bit messy, because the constructor
> parameters do not have names (unlike the setters). Additionally, generating
> tons of constructors for various combinations of optional dependencies is
> not nice.
>
> The requirement is to have the configuration in an XML file, therefore I
> have tried to use PicoContainer Script 2.0.1 this way:
>
> picoContainer = new DefaultPicoContainer(new CompositeInjection(new
> ConstructorInjection(), new SetterInjection()));
> final ClassLoader cL = Thread.currentThread().getContextClassLoader();
> new
> XMLContainerBuilder(cL.getResource(config),cL).populateContainer(picoContai
> ner);
>
> However, I am not able to rewrite the Spring XML file to the PicoContainer
> Script XML file and I start getting the feeling that such mixed dependency
> injection (constructor + optional setters) is not supportted by
> PicoContainer. Could someone please prove me wrong?

Hello Leos,

Welcome to PicoContainer land.

The bad news: Ok, so I can't actually help you figure out the XML syntax you
need.  

I usually use straight Java or Groovy Script to do what I need.  However, as to
whether PicoContainer can do the combination you're looking for, please take a
look at Composite Injection and see if this helps:

Docs:
http://www.picocontainer.org/composite-injection.html

Pertinent Test Case:
http://svn.codehaus.org/picocontainer/java/2.x/trunk/pico/container/src/test/org
/picocontainer/injectors/CompositeInjectionTestCase.java

HTH!
                                                                -Mike



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email