Hi everyone;

I am playing with the interesting DependencyManager toolkit for our dependency injection, and
I would like to provide a feedback concerning Auto Configuration ...

Indeed, I have observed that, when a required dependency is satisfied, DependencyManager always initialize Pojo's class attributes automatically (using Reflexion), even if "setCallbacks()"
method has been invoked.

Let met explain with the following sample code:
I have the following Pojo which requires a dependency over a java.util.HashMap object:

Here is my Pojo::

 public class MyPojo {
    protected HashMap _map1; // hashmap internally used by this class ...
    protected HashMap _map2; // hashmap internally used by this class ...
protected Object _param; // initialized from an injected java.util.HashMap

    protected void init() { // invoked by DependencyManager
      _map1 = new HashMap();
      _map1.put("foo", "bar");
      _map2 = new HashMap();
      _map2.put("foo2", "bar2");
    }

protected void inject(HashMap conf) { // invoked by DependencyManager when my conf is available
      // initialize my service using the injected conf map ...
      _param = conf.get("param");
    }

    protected void start() {
      // start my service ...
    }
 }

Here is my activator:

class Activator extends DependencyActivatorBase {
public void init(BundleContext ctx, DependencyManager mgr) throws Exception {
   mgr.add(createService()
   .setImplementation(MyPojo.class)
   .add(createServiceDependency()
        .setService(java.util.HashMap.class)
        .setCallbacks("inject")
        .setRequired(true)));
        // NOTICE THAT I HAVE NOT INVOKED setAutoConfig(false) ...
 }

But the previous sample code does not work because there is a hidden side effect: Indeed, my map attributes ("_map1", and "_map2") are automatically set by DependencyManager to the injected hashmap ! (and maps initialized in my init() method are just overriden). In order to fix this side effect, I have to invoke the "setAutoConfig(false)" method, like this:

class Activator extends DependencyActivatorBase {
public void init(BundleContext ctx, DependencyManager mgr) throws Exception {
   mgr.add(createService()
   .setImplementation(MyPojo.class)
   .add(createServiceDependency()
        .setService(java.util.HashMap.class)
        .setCallbacks("inject")
        .setRequired(true))
.setAutoConfig(false)); // Don't let DependencyManager set himself my two internal HashMap attributes (because they have nothing to do with the injected map
 }

I think that Explicit callbacks are typically used in place of AutoConfiguration, so it would seem natural to have AutoConfiguration *disabled* when explicit callbacks are defined (using setCallbacks method). The only reason I see for keeping both is when the specified callback has no argument and is only used as a listener, but then there are already other means to set service listeners.

-> What do you think ? Why AutoConfiguration is active by default (when setCallbacks() method is used) ?

Kind Regards;
/pierre

Reply via email to