泽西和谷歌Guice整合 [英] Jersey and Google Guice integration

查看:303
本文介绍了泽西和谷歌Guice整合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:为什么在创建JavaSE应用程序和ServletModule时需要创建AbstractModule,同时创建部署在某种类型的servlet容器(如jetty或tomcat)上的应用程序?他们之间有什么区别?



我需要将Jersey与Guice整合。有没有必要注册Guess for Jersey来使用它吗?我不能只是启动注射,并在我想要的地方(普通课程,过滤器,处理程序,服务,DAO等)吗?为什么我不能在JavaSE应用程序中配置guice,而是需要使用ServletModule?



就我在网上看到的,有很多使用示例HK2服务由Guice反之亦然,所以我可以认为它很重要吗? (必需的)



谢谢

解决方案

c> AbstractModule 是Guice的引导(配置)阶段的基本构建块。你总是需要一个或多个。另一方面,一个 ServletModule 是一个专门化的方法,它为您提供了一个在servlet容器中运行的事实。



Guice文档


此模块设置请求和会话范围,并提供一个
的地方来配置您的过滤器和servlet。




关于Guice-Jersey整合您当然需要设置它。它不会从蓝色的工作。作为任何其他依赖注入框架,Guice可以在控制构建对象时起作用。当有疑问问自己创建对象的时候。



与泽西和JAX-RS一般来说,谁创建对象?不是,你只是定义它们。容器创建它们。 JAX-RS运行时。在你的情况下,泽西运行时。而泽西岛则在内部使用HK2依赖注入框架。因此,您需要这两个框架,以便注入您使用某些Guice资源定义的JAX-RS类。或者相反的方式这就是为什么有一个 HK2-guice bridge 的原因。所以泽西岛将使用HK2建立您的物品,HK2也会在Guice上查找您的资源,感谢桥梁。



一个简单的例子。 我使用此代码初始化REST API,其中我想注入Guice资源。

  @ApplicationPath(api)
public class ApiRest extends ResourceConfig {
private static final Logger log = LoggerFactory.getLogger(ApiRest.class);

@Inject
public ApiRest(ServiceLocator serviceLocator,ServletContext servletContext){
log.debug(Inicialitzant Jersey);
包(net.sargue.app.api);

GuiceBridge.getGuiceBridge()。initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
注射器注射器=(注射器)servletContext.getAttribute(Injector.class.getName());
if(injector == null)
throw new RuntimeException(Guice Injector not found);
guiceBridge.bridgeGuice注射器(注射器);
}
}

请注意,上述示例需要 ServletModule 注册,因为它从 ServletContext 中提取Guice注射器。或者您可以将注入器添加到其他地方的 ServletContext 。或者在初始化REST API时创建注入器,这取决于您的首选项和应用程序。


My question is: why do I need to create AbstractModule when doing JavaSE application and ServletModule while creating application that is deployed on some kind of servlet container like jetty or tomcat? What are the differences between them?

I need to integrate Jersey with Guice. Is it necessary to register presence of Guice for Jersey to use it somehow? Can't I just enable injections and do them everywhere I want (normal classes, filters, handlers, services, DAOs etc.)? And why can't I just configure guice like in JavaSE application, but instead need to use ServletModule?

As far as I see on web, there are many examples of using HK2 services by Guice and vice versa, so I can consider it as important? (necessary?)

Thanks

解决方案

An AbstractModule is the basic building block of the bootstrap (configuration) phase of Guice. You always need one or more of that. On the other hand a ServletModule is an specialization which does some configuration for you given the fact that it is running in a servlet container.

From the Guice documentation:

This module sets up the request and session scopes, and provides a place to configure your filters and servlets from.

About the Guice-Jersey integration you certainly need to set it up. It won't work out of the blue. Guice, as any other dependency injection framework, works when it has the control of building your objects. When in doubt ask yourself who creates the object.

With Jersey, and JAX-RS in general, who creates the objects? Not you, you just define them. The container creates them. The JAX-RS runtime. In your case, the Jersey runtime. And Jersey uses internally the HK2 dependency injection framework. So you need to bridge both those frameworks in order to inject a JAX-RS class you have defined with some Guice resources. Or the other way around! That is the reason why there is a HK2-guice bridge. So Jersey would build your objects using HK2 and HK2 will look up your resources also on Guice thanks to the bridge.

A simple example. I use this code to initialize a REST API where I want to inject Guice resources.

@ApplicationPath("api")
public class ApiRest extends ResourceConfig {  
    private static final Logger log = LoggerFactory.getLogger(ApiRest.class);

    @Inject
    public ApiRest(ServiceLocator serviceLocator, ServletContext servletContext) {
        log.debug("Inicialitzant Jersey.");
        packages("net.sargue.app.api");

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        Injector injector = (Injector) servletContext.getAttribute(Injector.class.getName());
        if (injector == null)
            throw new RuntimeException("Guice Injector not found");
        guiceBridge.bridgeGuiceInjector(injector);
    }
}

Please note that the above example needs the ServletModule registered as it pulls the Guice injector from the ServletContext. Or you can just add the injector to the ServletContext somewhere else. Or just create the injector when initializing the REST API, it depends on your preferences and the application.

这篇关于泽西和谷歌Guice整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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