【问题标题】:How to inject object with HK2 in main method in a embedded Grizzly Jersey application如何在嵌入式 Grizzly Jersey 应用程序的 main 方法中使用 HK2 注入对象
【发布时间】:2015-11-18 21:03:34
【问题描述】:

我在使用 Jersey 设计 REST 微服务时遇到了 22 条问题。我正在尝试创建一个带有嵌入式灰熊服务器的应用程序以降低部署复杂性。因此,我有一个创建灰熊服务器的主要方法。我需要在服务器的引导过程之前注入一些对象。

我的主要看起来像这样:

public static void main(String[] args) {
    App app = new App(new MyResourceConfig());
    // Need to inject app here.
    app.init(args);
    app.start();        
}

如何获取 ServiceLocator 单例实例以便注入我的应用对象?

我试过了:

ServiceLocatorFactory.getInstance()
                     .create("whatever")
                     .inject(app);

但是,我需要将所有AbstractBinder 绑定到它上面两次(因为我已经在我的ResourceConfig 中这样做了)。

【问题讨论】:

    标签: java java-8 jersey-2.0 hk2


    【解决方案1】:

    将所有AbstractBinders 绑定到您正在创建的ServiceLocator,然后将该定位器传递给Grizzly 服务器创建工厂方法之一。这将是 Jersey 将从中提取所有服务并将它们粘贴到另一个定位器中的父定位器。例如像

    ServiceLocator serviceLocator = SLF.getInstance().create(...);
    ServiceLocatorUtilities.bind(serviceLocator, new Binder1(), new Binder2());
    App app = new App();
    serviceLocator.inject(app);
    HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
            URI.create(BASE_URI),
            resourceConfig, 
            serviceLocator
    );
    

    您需要确保您拥有 Jersey-Grizzly 依赖项,而不仅仅是 Grizzly。

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency> 
    

    另请参阅:

    注意:

    如果您尝试使用GrizzlyWebContainerFactory 创建一个 Grizzly servlet 容器,那么尝试完成上述工作将很困难。没有工厂方法可以传递定位器。 Jersey 确实添加了对属性 ServletProperties.SERVICE_LOCATOR 的支持,但即便如此,属性值也应该是 ServiceLocator。 grizzly servlet 容器的工厂方法只接受 Map&lt;String, String&gt; 的 init-params。这个属性实际上是由使用 Jetty 嵌入式的第三方贡献者添加的,它确实具有将值设置为对象的 init-params 方法。因此,如果您需要对此的支持,您可能需要向 Jersey 提出问题。

    【讨论】:

    【解决方案2】:

    为了扩展来自@peeskillet 的出色答案,在我的场景中,我必须将我的 Jersey 应用程序与其他 servlet 一起部署在同一个 Grizzly servlet 容器中,而且确实有点痛苦。

    但后来我找到了https://github.com/jersey/jersey/pull/128,它拯救了我的一天。看着那个拉取请求,这就是我想出的:

    WebappContext webappContext = new WebappContext("myWebappContext");
    
    webappContext.addListener(new ServletContextListener() {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            sce.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, MY_SERVICE_LOCATOR);
        }
        @Override
        public void contextDestroyed(ServletContextEvent sce) { }
    });
    
    ServletRegistration servlet = webappContext.addServlet("myAppplication", new ServletContainer(resourceConfig));
    servlet.addMapping("/application/*");
    
    ServletRegistration hello = webappContext.addServlet("myServlet", MyServlet.class);
    hello.addMapping("/servlet/*");
    
    HttpServer createHttpServer = GrizzlyHttpServerFactory.createHttpServer(MY_URI, false);
    webappContext.deploy(createHttpServer);
    createHttpServer.start();
    

    【讨论】:

      猜你喜欢
      • 2013-10-05
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2014-12-09
      • 1970-01-01
      相关资源
      最近更新 更多