2
我一直在努力解決這個問題一段時間了,所以我來到這裏與大家分享。 首先我有一個類至極我想注入的對象:@inject似乎不起作用NPE
public class MyClass {
@javax.inject.Inject
private MyInterface interface
/.../
public void myMethod(){
interface.doTask();
}
的MyInterface的:
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();
瞧,我很抱歉地問,但我需要對一些幫助,再加上我一直在努力,是儘可能通用,希望它不會影響你的理解。 在此先感謝!
編輯:忘了提,類MyClass的()被調用另一類是這樣的:new MyClass()
所以我覺得這個問題可能是在那裏。
這裏ResourceConfig的實現是什麼?這是在澤西島嗎? – jwells131313
你好,是的,這是在澤西島。 – hhelbawi