我正在寫一個簡單的微服務來公開REST API。所以我開始與澤西合作,當然我需要將我的對象注入球衣資源。基本上我有2個類定義了一組資源,其中一些需要使用另一個服務。Jersey HK2依賴注入
所以基本上我有:
public interface MyService {
String getServiceName();
void doService();
}這個接口的
2實施方式(MyServiceBean和MyAlternativeServiceBean)
和,據我理解讀球衣文檔,我所定義的HK2粘結劑:
public class MyBinder implements Binder{
@Override
public void bind(DynamicConfiguration config) {
DescriptorImpl descriptor = BuilderHelper.link(MyServiceBean.class).named("MyServiceBean").to(MyService.class).build();
config.bind(descriptor);
config.bind(BuilderHelper.link(MyAlternativeServiceBean.class).named("MyAlternativeServiceBean").to(MyService.class).build());
}
我註冊了這個活頁夾在ApplicationConfig類
public class ApplicationConfig extends ResourceConfig{
public ApplicationConfig(){
property("property.value", "MyAlternativeServiceImplementation");
registerInstances(new MyBinder());
}
}
並適當註釋到資源
@Path("first")
public class First {
@Inject @Named(value = "MyServiceBean")
private MyService myService;
//...
}
@Path("second")
public class Second {
@Inject @Named(value = "MyAlternativeServiceBean")
private MyService myService;
//...
}
所有作品,直至爲MyService實現無參數的構造函數。但在1例中,我需要爲MyAlternativeServiceBean提供一個依賴項。
這裏是構造
@Inject @Named("property.value")
public MyAlternativeServiceBean(String property){
this.property = property;
}
但我得到一個異常:
javax.servlet.ServletException: A MultiException has 5 exceptions. They are:|1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=String,parent=MyAlternativeServiceBean,qualifiers={}),position=0,optional=false,self=false,unqualified=null,2080509613)|2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.hpe.services.MyAlternativeServiceBean errors were found|3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.hpe.services.MyAlternativeServiceBean|4. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.hpe.tests.SecondEntryPoint errors were found|5. java.lang.IllegalStateException: Unable to perform operation: resolve on com.hpe.tests.SecondEntryPoint|
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:229)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:427)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:370)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:973)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1035)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:641)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:231)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:745)
基本上我卻不知道如何注入性能/常數(即我可以從一個配置文件中讀取實例)in hk2
謝謝
Regards
是什麼構造是什麼樣子?你期望這些構造函數從哪裏來? –
我想知道如何將球衣與hk2完全整合:我的意思是說hk2可以爲我建立對象,然後將這些對象注入球衣資源(當然也可以放入需要的其他對象中)。目前我不知道如何告訴hk2創建2個單例「MyServiceBean」和「MyAlternativeServiceBean」。我在想春天的方式,基本上框架爲我構建對象,並將這些對象保存到AppContext中。 – Raffaele
不知道你在得到什麼。該錯誤告訴你,它找不到合適的構造函數,因爲你添加了不適合注入點的構造函數參數,也就是說你沒有服務注入構造函數,或者你沒有將構造函數標記爲注入點'@ Inject'。這與Spring的工作方式沒有什麼不同,如果你嘗試使用構造函數參數,並且它沒有用'@ Autowired'標記爲注入點,或者沒有注入bean,你將會得到類似的錯誤。它也不例外。 –