我有一个缓存所有组件的PicoContainer。由于它缓存所有组件,所以我希望它在容器生命周期的适当点调用start、stop和dispose。
但是,我发现如果我使用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的适配器将被调用其方法,但目前还不清楚如何将其连接起来。如果我查看其他实现类,几乎所有的东西似乎都委托给了其他类,因此很难弄清楚到底发生了什么。
做这件事的正确方法是什么?有什么合适的方法吗?
发布于 2015-06-02 14:56:33
FactoryConstructedServiceAdapter应该实现LifecycleStrategy并拥有
@Override
public boolean hasLifecycle(Class<?> type) {
return true;
}基本上,工厂将包含在标准生命周期中,并且可以管理组件,而FactoryConstructedServiceImpl的实际实例化将被调用(如果您不需要工厂提供的组件的生命周期,只是想知道为什么没有实例化,请记住工厂是懒惰的,在实际连接或请求组件之前,您不会在日志中看到"FactoryConstructedServiceImpl#init“)。
如果您需要一个很大的例子,请以InstanceAdapter为例。
https://stackoverflow.com/questions/30585194
复制相似问题