如何使用 Jersey 将依赖项注入资源?

发布于 2024-09-30 23:33:09 字数 325 浏览 2 评论 0原文

我有以下代码:

@Path("stores")
class StoreResources {

  private ServerConfig config;

  @GET
  public String getAll() {
   //do some stuff with ServerConfig
  }
}

我需要将 ServerConfig 对象从外部注入到此类中,并在 getAll() 方法内使用它。

有哪些可能的方法来实现它?我应该使用 Guice 或 Spring 等 DI 框架吗?

I'm having the following code:

@Path("stores")
class StoreResources {

  private ServerConfig config;

  @GET
  public String getAll() {
   //do some stuff with ServerConfig
  }
}

And I need the ServerConfig object to be injected into this class from outside and use it inside the getAll() method.

What are the possible ways to achieve it? Should I use a DI framework like Guice or Spring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-10-07 23:33:09

这是一篇关于Jersey下Spring注入的好博客 http:// /javaswamy.blogspot.com/2010/01/making-jersey-work-with-spring.html

结果是您使用注释来标记要注入的字段,示例资源是

package com.km.services;  

import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import org.springframework.context.annotation.Scope;  
import org.springframework.stereotype.Component;  
import com.sun.jersey.spi.inject.Inject;  
import com.km.spring.SimpleBean;  

@Path("/hello")  
@Component  
@Scope("request")  
public class HelloResource {  

   @Inject private SimpleBean simpleBean;  

   @GET  
   @Produces("text/plain")  
   public String getMessage() {  
    return simpleBean.sayHello();  
   }  
} 

出于我的目的配置太困难了,所以我使用静态 spring 解析器工厂来解析 bean。例如。

private SimpleBean simpleBean = SpringBeanFactory.getBean("mySimpleBean");

This is a good blog about Spring injection under Jersey http://javaswamy.blogspot.com/2010/01/making-jersey-work-with-spring.html

The upshot is you use annotations to flag fields that are to be injected, an example resource being

package com.km.services;  

import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import org.springframework.context.annotation.Scope;  
import org.springframework.stereotype.Component;  
import com.sun.jersey.spi.inject.Inject;  
import com.km.spring.SimpleBean;  

@Path("/hello")  
@Component  
@Scope("request")  
public class HelloResource {  

   @Inject private SimpleBean simpleBean;  

   @GET  
   @Produces("text/plain")  
   public String getMessage() {  
    return simpleBean.sayHello();  
   }  
} 

For my purposes the configuration was excessively difficult so I used a static spring resolver factory to resolve the bean. eg.

private SimpleBean simpleBean = SpringBeanFactory.getBean("mySimpleBean");
冷了相思 2024-10-07 23:33:09

您不需要 Spring 或 Guice 来注入 ServletConfig。 Jersey 通过它自己的注入机制来实现。请参阅 Jersey 示例分发版附带的 simple-servlet 示例。以下是将 HttpServletRequest 和 ServletConfig 注入资源的示例代码:

@Path("/resource1")
public class ResourceBean1 {

    @Context
    HttpServletRequest servletRequest;

    @Context
    ServletConfig servletConfig;

    @GET
    @Produces("text/plain")
    public String describe() {
        return "Hello World from resource 1 in servlet: '" +
                servletConfig.getServletName() +
                "', path: '" +
                servletRequest.getServletPath() +
                "'";
    }

}

使用 Servlet 部署 JAX-RS 应用程序时,可使用 @Context 注入 ServletConfig、ServletContext、HttpServletRequest 和 HttpServletResponse。

You don't need Spring or Guice to inject a ServletConfig. Jersey does through its own injection mechanism. Refer to the simple-servlet example that comes with Jersey samples distribution. Here is the sample code that injects a HttpServletRequest and a ServletConfig onto a resource:

@Path("/resource1")
public class ResourceBean1 {

    @Context
    HttpServletRequest servletRequest;

    @Context
    ServletConfig servletConfig;

    @GET
    @Produces("text/plain")
    public String describe() {
        return "Hello World from resource 1 in servlet: '" +
                servletConfig.getServletName() +
                "', path: '" +
                servletRequest.getServletPath() +
                "'";
    }

}

When deploying an JAX-RS application using Servlet then ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse are available for injection using @Context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文