我有一个缓存所有组件的 PicoContainer。由于它缓存了所有组件,我希望它在容器生命周期的适当时间点调用start
, 。stop
dispose
但是,我发现如果我使用 a 构造一个组件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()
,我创建并作为实例注入的组件启动。我通过构造函数注入注入的组件被构造然后启动。但是从我通过工厂注入的组件中看不到任何东西。
就文档而言,shows 和 methods 的 Javadoc似乎FactoryInjector
是为工厂本身做自己的生命周期的东西,而不是工厂旋转出来的组件。#start
#stop
#dispose
快速查看源代码表明适配器实现ComponentLifecycle
将调用其方法,但尚不清楚如何将其挂钩。如果我查看其他实现类,几乎所有东西似乎都委托给其他东西,使其成为很难弄清楚到底发生了什么。
这样做的正确方法是什么?有没有合适的方法来做到这一点?