【发布时间】:2015-10-01 14:20:01
【问题描述】:
我已经为这个问题苦苦挣扎了一段时间,所以特地来和大家分享一下。 首先我有一个类,我想在其中注入一个对象:
public class MyClass {
@javax.inject.Inject
private MyInterface interface
/.../
public void myMethod(){
interface.doTask();
}
我的界面:
public interface MyInterface {
public abstract void doTask() throws Exception;
}
是我绑定到其实现的接口:
public class MyInterfaceImpl implements MyInterface{
@Inject
public MyInterfaceImpl(...) {
/.../
}
@Override
public void doTask() throws Exception{
/.../
}
在配置中:
public class ApplicationConfig extends ResourceConfig {
private Config config = new Config();
public ApplicationConfig() {
super();
register(new MainBinder());
}
private class MainBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyInterfaceImpl.class).to(MyInterface.class).in(Singleton.class);
}
}
}
所以,当我运行应用程序并尝试执行我有 NPE 的方法时:
interface.doTask();
Voilà,很抱歉问这个问题,但我需要一些帮助,此外,我已尝试尽可能笼统,希望它不会影响您的理解。
编辑:
忘了提到 MyClass() 类在另一个类中被调用,如下所示:new MyClass()
所以我认为问题可能就在那里。
【问题讨论】:
-
这里ResourceConfig的实现是什么?这是在泽西岛吗?
-
你好,是的,这是在泽西岛。
标签: java dependency-injection nullpointerexception jersey hk2