带有参数的Jersey构造函数 [英] Jersey constructor with parameters

查看:125
本文介绍了带有参数的Jersey构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用Jersey开发的RESTful服务中使用DAO,因此应通过服务的构造函数注入DAO实现:

I want to use a DAO in my RESTful service developed with Jersey, so the DAO implementation should be injected via the constructor of the service:

@Path("eventscheduler)
public class EventSchedulerService {
    private IEventSchedulerDao dao;

    public EventSchedulerService(IEventSchedulerDao dao) { this.dao = dao; }
}

但是,我知道Jersey希望默认的构造函数能够正确设置所有内容.我一直在努力寻找方法,但是令人惊讶的是,这似乎是一个罕见的案例,我想知道人们是如何将DAO注入其服务中的,或者根本上不涉及注入的.

However, I know Jersey expects a default constructor to setup everything correctly. I have been trying to figure out how to do this for a while but surprisingly this seems to be an uncommon case, I wonder how people inject DAOs into their services, or deal with injection in general at all.

我该怎么做?

推荐答案

如果您使用的是Jersey 2,它将使用 HK2 ,因为它是DI框架.所有资源类在构造时都会经历DI生命周期.而且构造函数注入不是问题.

If you're using Jersey 2, it uses HK2 as it's DI framework. All resource classes go through the DI lifecycle when they are constructed. And constructor injection is not a problem.

(使用Jersey的)使任意对象可注射的最基本方法是绑定到AbstractBinder

The most basic way (with Jersey) to make an arbitrary object injectable, is to bind in an AbstractBinder

public class Binder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(EventSchedudlerDaoImpl.class).to(EventSchedulerDao.class);
    }
}

然后在泽西岛注册活页夹

Then register the binder with Jersey

public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(new Binder());
    }
}

然后,您只需要通过在构造函数的顶部添加@Inject来声明注入点.

Then you just need to declare the injection point by adding @Inject on top of the constructor.

@Inject
public EventSchedulerService(EventSchedulerDao dao) {
    this.dao = dao;
}

就绑定器实现而言,绑定语法基本上读为

As far as the binder implementation, the binding syntax basically reads as

bind( Implementation ).to( Contract ).in( Scope );

bind方法可以采用实例,也可以采用类.提供实例时,作用域将自动为Singleton.

The bind method can take an instance or it can take a class. When you provide an instance, the Scope is automatically Singleton.

to方法指定公布的合同,该合同是可以在注入点声明的类型.在这种情况下,只能将接口EventSchedulerDao用作注入点.如果没有界面,就可以

The to method specifies the advertised contract, which is the type that can be declared at the injection point. In this case, only the interface EventSchedulerDao can be used for the injection point. If you don't have an interface you can just do

bindAsContract(EventSchedulerDao.class)

假设EventSchedulerDao是实现类.

可用的范围是PerLookupRequestScopedSingleton.如果未指定,则默认范围为PerLookup,这意味着将为每个注入点创建服务的新实例.您应该已经知道Singleton的含义. RequestScoped意味着将为每个请求创建一个新实例,该实例可能与PerLookup不同,因为该服务可能会在整个请求生命周期的多个点被注入.

The scopes available are PerLookup, RequestScoped and Singleton. If not specified, the default scope will be PerLookup, meaning a new instance of the service will be created for each injection point. You should already know what Singleton means. RequestScoped means that a new instance will be created for each request, which may not be the same as PerLookup, as the service may be injected at multiple points through out the request lifeclyle.

另请参见:

  • Dependency injection with Jersey 2.0
  • Custom Injection and Lifecycle Management
  • hk2 tagged questions. Alot of them are Jersey related.

这篇关于带有参数的Jersey构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆