gpt4 book ai didi

java - 如何让 PicoContainer 启动/停止/处置工厂注入(inject)的组件?

转载 作者:行者123 更新时间:2023-11-30 08:09:33 26 4
gpt4 key购买 nike

我有一个 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() 上,我创建并作为实例注入(inject)的组件启动了。我通过构造函数注入(inject)注入(inject)的组件被构造然后启动。但从我通过工厂注入(inject)的组件中看不到任何东西。

就文档而言,FactoryInjector 的 Javadoc 显示了 #start#stop#dispose 方法,这些方法似乎是为了让工厂本身做自己的生命周期的事情,而不是为了工厂派生出来的组件。

快速查看源代码表明,实现 ComponentLifecycle 的适配器将调用其方法,但目前尚不清楚如何 Hook 它。如果我查看其他实现类,实际上,所有事情似乎都委托(delegate)给了其他事情,因此很难弄清楚到底发生了什么。

执行此操作的正确方法是什么?有没有正确的方法来做到这一点?

最佳答案

FactoryConstructedServiceAdapter 应实现 LifecycleStrategy并且有

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

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

如果您需要一个大例子,请使用 InstanceAdapter。

关于java - 如何让 PicoContainer 启动/停止/处置工厂注入(inject)的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com