如何让PicoContainer启动/停止/处理工厂注入的组件?

时间:2015-06-01 23:51:51

标签: java dependency-injection factory picocontainer

我有一个PicoContainer可以缓存所有组件。由于它会缓存所有组件,因此我希望它能够在容器生命周期的适当位置调用startstopdispose

但是,我发现如果使用FactoryInjector构建组件,这些方法根本不会被调用,尽管该组件也被缓存。

采用以下示例:

import java.lang.reflect.Type;

import org.picocontainer.Characteristics;
import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.Disposable;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoContainer;
import org.picocontainer.Startable;
import org.picocontainer.injectors.FactoryInjector;

public class PicoContainerFactoryTest {
    public static void main(String[] args) {
        MutablePicoContainer container =
            new DefaultPicoContainer().as(Characteristics.CACHE);
        try {
            System.out.println("Adding components...");
            container.addComponent(InstanceService.class,
                                   new InstanceServiceImpl());
            container.addComponent(ConstructedService.class,
                                   ConstructedServiceImpl.class);
            container.addAdapter(
                new FactoryConstructedServiceAdapter());

            System.out.println("Starting...");
            container.start();

            // Even this doesn't trigger it. :(
            //container.getComponent(FactoryConstructedService.class);

            System.out.println("Stopping...");
            container.stop();
        }
        finally
        {
            System.out.println("Disposing...");
            container.dispose();
        }
    }


    public interface InstanceService
        extends Startable, Disposable {}
    public interface ConstructedService
        extends Startable, Disposable {}
    public interface FactoryConstructedService
        extends Startable, Disposable {}

    private static class InstanceServiceImpl extends Impl
            implements InstanceService {
        public InstanceServiceImpl() {
            super("InstanceServiceImpl");
        }
    }

    public static class ConstructedServiceImpl extends Impl
            implements ConstructedService {
        public ConstructedServiceImpl() {
            super("ConstructedServiceImpl");
        }
    }

    private static class FactoryConstructedServiceAdapter
            extends FactoryInjector<FactoryConstructedService> {

        public FactoryConstructedServiceAdapter() {
            super(FactoryConstructedService.class);
        }

        @Override
        public FactoryConstructedService getComponentInstance(
            PicoContainer picoContainer, Type type) {
            return new FactoryConstructedServiceImpl();
        }

        private static class FactoryConstructedServiceImpl extends Impl
            implements FactoryConstructedService {
            public FactoryConstructedServiceImpl() {
                super("FactoryConstructedServiceImpl");
            }
        }
    }

    public static class Impl implements Startable, Disposable {
        private final String name;

        public Impl(String name) {
            this.name = name;
            System.out.println("  " + name + "#<init>");
        }

        @Override
        public void start() {
            System.out.println("  " + name + "#start");
        }

        @Override
        public void stop() {
            System.out.println("  " + name + "#stop");
        }

        @Override
        public void dispose() {
            System.out.println("  " + name + "#dispose");
        }
    }
}

运行它的输出如下:

Adding components...
  InstanceServiceImpl#<init>
Starting...
  ConstructedServiceImpl#<init>
  InstanceServiceImpl#start
  ConstructedServiceImpl#start
Stopping...
  ConstructedServiceImpl#stop
  InstanceServiceImpl#stop
Disposing...
  ConstructedServiceImpl#dispose
  InstanceServiceImpl#dispose

因此,在start()上,我创建并作为实例注入的组件已启动。我通过构造函数注入注入的组件构建然后启动。但是我从工厂注入的组件中没有看到任何东西。

就文档而言,FactoryInjector的Javadoc显示#start#stop#dispose方法,这些方法似乎是供工厂自己使用的拥有生命周期的东西,而不是工厂旋转的组件。

快速查看源代码显示实现ComponentLifecycle的适配器将调用其方法,但它不会立即清除如何将其挂钩。如果我查看其他实现类,实际上一切似乎都委托给别的东西,很难弄清楚究竟发生了什么。

这样做的正确方法是什么?有没有正确的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

FactoryConstructedServiceAdapter应该实现LifecycleStrategy

@Override
public boolean hasLifecycle(Class<?> type) {
    return true;
}

基本上,就是这样,工厂将被包含在标准生命周期中并且可以管理组件并且将调用FactoryConstructedServiceImpl的实际实例化(如果您不需要工厂提供的组件的生命周期,并且只是想知道为什么它是没有实例化,请记住工厂是懒惰的,并且在实际连接或请求组件之前,您不会在日志中看到“FactoryConstructedServiceImpl #init”。

如果您需要一个很好的例子,请选择InstanceAdapter。