【发布时间】:2018-03-30 00:21:01
【问题描述】:
我正在尝试了解 Jersey 应用程序中的 HK2 Factory 实现。
目标:如何实现单例工厂?
// Below is the simple factory implementation
public class MyFactory implements Factory<SomeObject> {
private static final Logger logger = LoggerFactory.getLogger(MyFactory.class);
private final CloseableService closeService;
@Inject
public MyFactory(CloseableService closeService) {
this.closeService = closeService;
}
@Override
public MyFactory provide() {
logger.debug("provide object from MyFactory");
SomeObject objectFromFactory = new SomeObject();
this.closeService.add(() -> dispose(objectFromFactory));
return objectFromFactory;
}
@Override
public void dispose(SomeObject instance) {
// destroy instance
logger.debug("dispose object from MyFactory");
}
}
// and binding
bindFactory(MyFactory.class).to(SomeObject.class).in(Singelton.class);
// to check object creation and destruction
bind(HK2InstanceListener.class).to(InstanceLifecycleListener.class).in(Singleton.class);
// and injecting in some resource class
@Inject
SomeObject useThisObject;
//updated information
AbstractBinder abstractBinder = configureBinder();
ServiceLocator appServiceLocator = configureHK2(abstractBinder);
public static AbstractBinder configureBinder() {
return new AbstractBinder() {
@Override
protected void configure() {
bindFactory(MyFactory.class).to(SomeObject.class).in(Singelton.class);
// to check object creation and destruction
bind(HK2InstanceListener.class).to(InstanceLifecycleListener.class).in(Singleton.class);
}
}
public ServiceLocator configureHK2(AbstractBinder binder) {
ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
ServiceLocator locator = factory.create("my-test-server");
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
locator.inject(binder);
binder.bind(dc);
dc.commit();
return locator;
}
在启动应用程序时,我在日志中看到以下内容
10:38:34.122 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 before object create : com.test.MyFactory
10:38:34.125 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 before object create : com.test.MyFactory
10:38:34.125 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 after object create : com.test.MyFactory
10:38:34.125 [grizzly-http-server-0] DEBUG com.test.MyFactory provide - object from MyFactory
10:38:35.700 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 after object create : com.test.MyFactory
10:38:37.743 [grizzly-http-server-0] DEBUG com.test.MyFactory - dispose object from MyFactory
-
当范围 = 单例时
- 创建两个 MyFactory 对象
- 下一个请求失败,出现空指针异常@Inject。
-
当范围 = RequestScoped,PerLookup 时
- 每个请求都会创建两个 MyFactory 对象
通过单例工厂,我理解是工厂的单个对象(MyFactory),它在注入时提供某种对象。
那么(1)应该可以工作还是我错过了什么?
为什么有两个工厂对象?
有什么建议吗?提前致谢。
HK2 Version : 2.5.0-b60
Jersey Version: 2.26
关于 NPE 的其他信息
它不是来自 HK2,但 .in(Singleton.class) 和 .in(PerLookup.class)
之间的行为不同// SomeObject looks like
Class SomeObject
{
private Stack<String> someStack;
public SomeObject() {
// this may be the issue for Singleton
this.someStack = new Stack();
}
public someOperation(String stackIt)
{
// NPE location
this.someStack.push(stackIt);
}
}
NPE 在上方时在下方
bindFactory(MyFactory.class,Singleton.class).to(SomeObject.class).in(Singleton.class);
下方时上方位置无NPE
bindFactory(MyFactory.class,Singleton.class).to(SomeObject.class).in(PerLookup.class);
【问题讨论】:
-
你的工厂是怎么绑定的?你如何绑定它可能是你为什么看到你所看到的行为的答案
-
@jwells131313 用绑定信息更新了帖子。
标签: jersey-2.0 hk2