gpt4 book ai didi

java - Jersey 2.0 的依赖注入(inject)

转载 作者:IT老高 更新时间:2023-10-28 11:26:44 27 4
gpt4 key购买 nike

在没有任何以前的 Jersey 1.x 知识的情况下从头开始,我很难理解如何在我的 Jersey 2.0 项目中设置依赖注入(inject)。

我也知道 HK2 在 Jersey 2.0 中可用,但我似乎找不到有助于 Jersey 2.0 集成的文档。

@ManagedBean
@Path("myresource")
public class MyResource {

@Inject
MyService myService;

/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getit")
public String getIt() {
return "Got it {" + myService + "}";
}
}

@Resource
@ManagedBean
public class MyService {
void serviceCall() {
System.out.print("Service calls");
}
}

pom.xml

<properties>
<jersey.version>2.0-rc1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<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.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jax-rs-ri</artifactId>
</dependency>
</dependencies>

我可以让容器启动并提供我的资源,但只要我将@Inject 添加到 MyService,框架就会引发异常:

SEVERE: Servlet.service() for servlet [com.noip.MyApplication] in context with path [/jaxrs] threw exception [A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MyService,parent=MyResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.noip.MyResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.noip.MyResource
] with root cause
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MyService,parent=MyResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)


我的入门项目在 GitHub 上可用:https://github.com/donaldjarmstrong/jaxrs

最佳答案

您需要定义一个 AbstractBinder 并将其注册到您的 JAX-RS 应用程序中。 binder 指定依赖注入(inject)应如何创建您的类。

public class MyApplicationBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyService.class).to(MyService.class);
}
}

当在 MyService.class 类型的参数或字段上检测到 @Inject 时,它会使用类 MyService 进行实例化。要使用此绑定(bind)器,需要向 JAX-RS 应用程序注册。在您的 web.xml 中,像这样定义一个 JAX-RS 应用程序:

<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mypackage.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

实现 MyApplication 类(在上面的 init-param 中指定)。

public class MyApplication extends ResourceConfig {
public MyApplication() {
register(new MyApplicationBinder());
packages(true, "com.mypackage.rest");
}
}

指定依赖注入(inject)的绑定(bind)器在类的构造函数中注册,我们还使用 packages( ) 方法调用。

关于java - Jersey 2.0 的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16216759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com