我正在写一个简单的微服务来公开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,你将会得到类似的错误。它也不例外。 –