对于我的生活,我无法通过hk2获得Jersey来自动发现@Service注释类并注入它们。我试图遵循堆栈溢出,泽西和hk2文档的每一个建议,仍然没有运气。我试图将一个简单的echo服务注入Jersey资源。骨架是从Jersey的简单webapp maven原型生成的,我试图扩展它。这就是我到目前为止所做的:
的pom.xml
<build>
<finalName>sandbox</finalName>
<plugins>
<plugin>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-inhabitant-generator</artifactId>
<version>2.3.0</version>
<executions>
<execution>
<configuration>
<verbose>true</verbose>
</configuration>
<goals>
<goal>generate-inhabitants</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
的web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.package.jerseytest</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>my.package.jerseytest.application.Application</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
my.package.jerseytest.application.Application
public class Application extends ResourceConfig {
public Application() {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
}
}
my.package.jerseytest.service.EchoService
@Service
public class EchoService {
public String generateResponse(String echo) {
return echo;
}
}
my.package.jerseytest.resource.MyResource
@Path("myresource")
public class MyResource {
@Inject
EchoService echoService;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return echoService.generateResponse("Got it!");
}
}
我已经检查过,抑制剂生成器确实运行并产生了它的输出,但是在运行Tomcat服务器时GETting http://localhost:8080/sandbox/webapi/myresource
我得到了
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/sandbox] threw exception [A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=EchoService,parent=MyResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,932014249)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of my.package.jerseytest.resource.MyResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on my.package.jerseytest.resource.MyResource
] with root cause
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=EchoService,parent=MyResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,932014249)
我缺少什么想法?我将不胜感激任何帮助:(
NB!我知道
但他们没有帮助我......
答案 0 :(得分:1)
我结合了从这两个问题中获得的见解:
首先,在构建链中使用HK2 Metadata Generator(或居民生成器)(就像您一样)。这将扫描您的来源并创建Dipose()
。
其次,创建一个新的META-INF/hk2-locator/default
,填充元数据中的服务:
ServiceLocator
现在将其传递给ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
。引用@peeskillet:
泽西岛拥有自己的ServiceLocator,尝试获取它的参考并不容易。我们可以给泽西岛我们的ServiceLocator,但泽西岛最终仍然创建它自己的定位器,并用我们的定位器填充它。
Grizzly
答案 1 :(得分:1)
通过使用扩展AbstractBinder的类,实例化它并将其注册到应用程序中,我解决了我的问题,就像这个问题一样。
resourceConfig.register(new DependencyBinder());
此外,
/**
* dependency injection bindings.
* Jersey requires that service implementations are bound to their contracts this way.
*/
public final class DependencyBinder extends AbstractBinder {
@Override
protected final void configure() {
bind(StatusServiceImpl.class).to(StatusService.class);
}
}
答案 2 :(得分:0)
尝试在Application构造函数中添加需要扫描的包。包上的“true”参数表示以递归方式扫描包:
public class Application extends ResourceConfig {
public Application() {
packages(true, "my.package.jerseytest");
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
}
}
答案 3 :(得分:0)
使用packages(true, "my.package.jerseytest");
并使用org.glassfish.jersey.spi.Contract
而不是org.jvnet.hk2.annotations.Contract
注释。
并且使用没有泛型的简单接口。
答案 4 :(得分:0)
尝试添加@Stateless
@Path("myresource")
@Stateless
public class MyResource {
@Inject
EchoService echoService;
...
}