2016-08-25 128 views
0

我有一個基於Jersey 2,Guice 3.0技術的項目。 由於Jersey 2框架使用HK2,我不得不配置HK2-Guice橋。如何配置HK2以使用com.google.inject包中的Guice @Inject註釋?

JerseyGuiceServletContextListener:

package com.example.core; 

import com.google.inject.Guice; 
import com.google.inject.Injector; 
import com.google.inject.servlet.GuiceServletContextListener; 
import com.google.inject.servlet.ServletModule; 
public class JerseyGuiceServletContextListener extends GuiceServletContextListener { 

    static Injector injector; 

    @Override 
    protected Injector getInjector() { 
     injector = Guice.createInjector(new ServletModule() { 
      @Override 
      protected void configureServlets() { 
       //some code here 
      } 
     }); 
     return injector; 
    } 
} 

JerseyConfiguration:

package com.example.core; 

import org.glassfish.hk2.api.ServiceLocator; 
import org.glassfish.jersey.server.ResourceConfig; 
import org.glassfish.jersey.server.ServerProperties; 
import org.jvnet.hk2.guice.bridge.api.GuiceBridge; 
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge; 

import javax.inject.Inject; 
//... 

class JerseyConfiguration extends ResourceConfig { 

    @Inject 
    public JerseyConfiguration(ServiceLocator serviceLocator) { 
     packages("com.example.ws"); 

     GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator); 
     GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class); 
     guiceBridge.bridgeGuiceInjector(JerseyGuiceServletContextListener.injector); 
    } 
} 

所有,如果我使用javax.inject.Inject註解注入一些類屬性工作正常。但是,當我使用com.google.inject.Inject註釋我的類屬性不注入&當我試圖使用此屬性時,我得到NullPointerException。

如何使用com.google.inject.Inject註釋?

回答

0

您將爲com.google.inject.Inject創建您自己的InjectionResolver。下面的例子將創建一個,正如,比如javax.inject.Inject。不過,在下面的實現中不會捕獲到Google Inject和javax Inject之間的細微差別。如果您依賴任何這些差異,您可能需要在實現InjectionResolver時變得更加智能。然後,您還需要在Jersey綁定呼叫或另一個Jersey綁定呼叫中註冊您的InjectionResolver。以下是基本實現:

@Singleton 
public class GuiceInjectResolver implements InjectionResolver<com.google.inject.Inject> { 
    @Inject @Named(InjectionResolver.SYSTEM_RESOLVER_NAME) 
    private InjectionResolver<Inject> systemResolver; 

    /* (non-Javadoc) 
    * @see org.glassfish.hk2.api.InjectionResolver#resolve(org.glassfish.hk2.api.Injectee, org.glassfish.hk2.api.ServiceHandle) 
    */ 
    @Override 
    public Object resolve(Injectee injectee, ServiceHandle<?> root) { 
     return systemResolver.resolve(injectee, root); 
    } 

    /* (non-Javadoc) 
    * @see org.glassfish.hk2.api.InjectionResolver#isConstructorParameterIndicator() 
    */ 
    @Override 
    public boolean isConstructorParameterIndicator() { 
     return true; 
    } 

    /* (non-Javadoc) 
    * @see org.glassfish.hk2.api.InjectionResolver#isMethodParameterIndicator() 
    */ 
    @Override 
    public boolean isMethodParameterIndicator() { 
     return true; 
    } 

} 
+0

有關InjectionResolvers的更多信息可以在這裏找到:https://hk2.java.net/2.5.0-b08/extensibility.html#Custom_Injection_Resolvers – jwells131313

+0

但正如我所定義的橋hk2-guice - 我不能只使用谷歌的註釋,因爲它們是?沒有InjectionResolver impl。 – Alexiuscrow

+0

橋只允許Guice看到HK2服務和HK2看到Guice服務。 InjectionResolver將允許您在HK2創建的服務中使用Guice註釋(這必須是您的示例中發生的情況) – jwells131313

相關問題