Guice don't inject to Jersey's resources

前端 未结 2 1434
Happy的楠姐
Happy的楠姐 2020-12-18 11:53

Parsed allover the whole internet, but can\'t figure out why this happens. I\'ve got a simplest possible project (over jersey-quickstart-grizzly2 archetype) with one Jersey

2条回答
  •  攒了一身酷
    2020-12-18 12:34

    Yes, in my opinion the answer above is correct. A very good way is to bridge Guice to HK2. I am not 100% sure if it neccessary to create a new TransportModule in the code above. Indeed Guice registers its injector in the ServletContext with the class name of Injector anyway, so it can be fetched from there:

            register(new ContainerLifecycleListener() {
            public void onStartup(Container container) {
                ServletContainer servletContainer = (ServletContainer)container;
                ServiceLocator serviceLocator = container.getApplicationHandler().getServiceLocator();
                GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
                GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
                Injector injector = (Injector) servletContainer.getServletContext().getAttribute(Injector.class.getName());
                guiceBridge.bridgeGuiceInjector(injector);
            }
            public void onReload(Container container) {
            }
            public void onShutdown(Container container) {
            }       
        });
    

    Beside that it is not trivial how to configure the REST Endpoints in that way, so I wrote detailled blog about that here.

提交回复
热议问题