Hi all,

normally you just use dependency injection to inject an object into another one. This works as long as you can work with one injected instance.

There are some cases though were you want a fresh object each time that is created from a blueprint (or similar) definition. One such case in the karaf code is for the SShServer and SshServerAction. There we currently want to create and start a new server each time the command is called. So while I am not sure if this is the best example I think the need for it is there.

So currently we do the following to create the new object:
SshServer server = (SshServer) container.getComponentInstance(sshServerId); So we inject the BlueprintContainer and the component id into the SshServerAction and then call a container method to create the instance.

This makes the SshServerAction class dependent on blueprint and also violates the dependency injection principle.

So I wonder if there would be a better way to do it.
If not I have got an idea how to achieve that:

We could create a GenericFactory interface like this:
public interface GenericFactory<T> {
    public T create();
}

So the class that wants to create the object would have a property:

private GenericFactory<SshServer> serverFactory;
...
SshServer server = serverFactory.create();

So we would simply have to find a way to inject an implementation of the GenericFactory interface into the object. We could just use a normal property definition and detect that it is a factory and then create a dynamic proxy for the factory.
<property name="serverFactory" ref="sshServer"/>

Another way would be to use an annotation:
@Inject(ref="sshServer")
private GenericFactory<SshServer> serverFactory;

So what do you think? Would it make sense to add this to aries? Or is it already possible and I just did not find it?

Christian

--
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com

Reply via email to