HK2等效辅助注射与FactoryModuleBuilder

时间:2015-01-22 10:08:50

标签: java dependency-injection jersey-2.0 hk2

感谢移植到球衣2,我需要从guice迁移到HK2。对于我的一些依赖项,我有一个辅助注入方法,我无法在HK2中实现。它看起来应该通过Custom Injection Resolvers来解决,但我真的不知道如何解决。这些例子对我来说不够清楚..

以下是Guice的观点:

public interface MyFactory {
    public MyClass createMyClass(@Assisted String dynamicParameter);
    public HisClass createHisClass(@Assisted String dynamicParameter);
    ...
}

binder.install(new FactoryModuleBuilder().build(MyFactory.class));

public class MyClass {
   ...
   @Inject
   public MyClass(@Assisted String dynamicParameter, SomeService someOtherServiceInjectedAutomatically){
      ...
   }
}

我如何在HK2上实现这个?

2 个答案:

答案 0 :(得分:2)

在发布问题之后我想到了这样做:

public class MyFactoryImpl implements MyFactory{

   private final SomeService someService;

   @Inject
   public MyFactoryImpl(SomeService someService){
      this.someService = someService;
   }

   public MyClass createMyClass(String dynamicParameter){
      return new MyClass(dynamicParameter, someService);
   }

   ...
}

答案 1 :(得分:1)

有一个Guice-Bridge: - D

<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>guice-bridge</artifactId>
    <version>${hk2.version}</version>
</dependency>

以下是使用Guice 3.0和HK2 2.3.0(与Jersey 2.13捆绑在一起)的示例。这只是一个独立的,但它应该在Jersey环境中工作。

Guice课程

public class GuiceGreeter {
    public String getGreeting(String name) {
        return "Hello, " + name;
    }
}

import com.google.inject.assistedinject.Assisted;
import javax.inject.Inject;

public class Message {
    private final String message;  
    @Inject
    public Message(GuiceGreeter greeter, @Assisted String name) {
        message = greeter.getGreeting(name);
    }
    public String getMessage() {
        return message;
    }
}

public interface GuiceMessageFactory {
    public Message getMessage(String name);
}

import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;

public class GuiceMessageModule extends AbstractModule {
    @Override
    protected void configure() {
        install(new FactoryModuleBuilder().build(GuiceMessageFactory.class));
        bind(GuiceGreeter.class);
    }   
}

HK2服务,注入Guice工厂

import javax.inject.Inject;

public class HK2Service {
    private final GuiceMessageFactory messageFactory;

    @Inject
    public HK2Service(GuiceMessageFactory messageFactory) {
        this.messageFactory = messageFactory;
    }

    public void printMessage(String name) {
        System.out.println(messageFactory.getMessage(name).getMessage());
    }
}

主要

import com.google.inject.Guice;
import com.google.inject.Injector;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;

public class Main {
    public static void main(String[] args) {
        // Create service locator. In Jersey context, you should be able to
        // inject the `ServiceLocator` into the `Application/ResourceConfig` 
        // subclass constructor, or as a field
        ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
        ServiceLocator locator = factory.create("SimpleServiceLocator");

        // bridge the two frameworks to allow Guice injected services
        GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
        Injector injector = Guice.createInjector(new GuiceMessageModule());
        GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(injector);

        // Add my HK2 Service
        ServiceLocatorUtilities.addClasses(locator, HK2Service.class);

        // Look up HK2 service. If this lookup works, `@Inject` in Jersey should.
        HK2Service service = locator.getService(HK2Service.class);
        service.printMessage("peeskillet");
    }
}

打印出"Hello, peeskillet"。请参阅以下main方法下面的评论,以获取Jersey应用中的ServiceLocator。如果您使用ServiceLocator进行无法更新,则使用AbstractBinder添加的所有绑定也将被放入服务定位器上下文中,因此您不必像我一样显式添加类与HK2Service一起走到上面。