0

我有一个应用程序将 Jersey/JAX-RS 用于 Web 服务(注释等)和 Guice 来注入服务实现。我不太喜欢 Guice 直接使用 servlet 的方式,我更喜欢 Jersey 方式,所以我不得不做一些小题大做才能让服务注入工作,因为 Guice 不会创建我的 servlet 类,而我没有不想处理HK2-Guice桥。我通过创建一个侦听器类(称为配置)在应用程序启动时在静态字段中设置注入器来做到这一点,然后通过创建一个父类来手动影响每个 servlet 类中的注入,我的所有 servlet 使用包含以下内容的构造函数扩展该父类:

public MasterServlet() {
    // in order for the Guice @Inject annotation to work, we have to create a constructor
    // like this and call injectMembers(this) on all our injectors in it
    Configuration.getMyServiceInjector().injectMembers(this);
    Configuration.getDriverInjector().injectMembers(this);
}

我知道这有点 hacky,但这在我的 servlet 中工作得很好。我可以在我的服务上使用 Guice @Inject 注释并在命名实现之间切换等等。当我去设置我的单元测试时,问题就来了。我正在使用 JerseyTest 进行测试,但是针对我的 servlet 运行测试会导致 Guice 出现 500 错误,如下所示:

com.google.inject.ConfigurationException:Guice 配置错误:1) 没有绑定 com.mycompany.MyService 的实现。在为 com.mycompany.servlet.TestGetServlet.service(TestGetServlet.java:21) 的字段定位 com.mycompany.MyService 的同时定位 com.mycompany.servlet.TestGetServlet

测试看起来像这样:

public class TestServletTest extends JerseyTest {

    @Test
    public void testServletFunctional() {
        final String response = target("/testget").request().get(String.class);
        assertEquals("get servlet functional", response);
    }

    @Before
    public void setup() {
        Configuration configuration = new Configuration();
        configuration.contextInitialized(null);
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(TestGetServlet.class);
    }
}

您会注意到在 setup 方法中我手动创建了我的 Configuration 类,因为我不能依赖测试容器(Grizzly)来创建它(我得到 NullPointerExceptions 没有这两行)。下面有更多关于这个的信息。

这是正在测试的 servlet:

@Path("/testget")
public class TestGetServlet extends MasterServlet {

    @Inject
    MyService service;

    @GET
    @Produces({"text/plain", MediaType.TEXT_PLAIN})
    public String testGet() {
        //service = Configuration.getServiceInjector().getInstance(MyService.class);
        return "get servlet functional";
    }
}

注意 testGet() 方法中的注释行吗?如果我这样做并删除上面的 @Inject 注释,一切正常,这表明 Grizzly 没有按照我期望的方式创建我的 servlet。

我认为正在发生的事情是灰熊不知道Guice。一切似乎都表明 Grizzly 没有看到 Configuration 类,尽管通过将它放在我的测试的 @Before 方法中,它似乎至少可供使用它的类使用(参见:TestGetServlet 类中的注释行)。我只是不知道如何解决它。

4

1 回答 1

2

我仍在努力解决这个问题,但与此同时,我从 Guice 切换到 HK2,这需要做一些工作,但我认为这可能对将来遇到此问题的任何人有所帮助。

我认为这是一个答案,因为老实说,我试图绕过 Guice-HK2 桥但仍将 Guice 与 Jersey 一起使用可能不是最好的主意。

从 Guice 切换到 HK2 需要做一些工作,并且没有包含所有答案的全面指南。例如,依赖关系真的很挑剔。如果您尝试使用 Jersey 2.27,您可能会遇到著名的

java.lang.IllegalStateException:找不到 InjectionManagerFactory

错误。由于 HK2 本身,Jersey 2.27 不向后兼容以前的版本。我仍在努力让这一切正常工作,但与此同时,我不得不将我所有的 Jersey 依赖项降级到 2.26-b06 以使 HK2 正常工作。

谢天谢地,泽西岛已经实现了一堆 HK2 样板,所以你只需要正确使用 @Contract、@Service(参见 HK2 文档),然后是两个看起来像这样的新类:

public class MyHK2Binder extends AbstractBinder {
    @Override
    protected void configure() {
        // my service here is a singleton, yours might not be, so just omit the call to in()
        // also, the order here is switched from Guice! very subtle!
        bind(MyServiceImpl.class).to(MyService.class).in(Singleton.class);
    }
}

和这个:

public class MyResourceConfig extends ResourceConfig {

    public MyResourceConfig() {
        register(new MyHK2Binder());
        packages(true, "com.mycompany");
    }
}

很简单,但这仅适用于应用程序本身。测试容器对此一无所知,因此您必须在测试类中自己重做 Binder 和 ResourceConfig,如下所示:

public class TestServletTest extends JerseyTest {

    @Test
    public void testServletFunctional() {

        final String response = target("/testget").request().get(String.class);
        assertEquals("get servlet functional", response);
    }

    @Before
    public void setup() {
    }

    @Override
    protected Application configure() {
        return new TestServletBinder(TestGetServlet.class);
    }

    public class TestServletBinder extends ResourceConfig {
        public TestServletBinder(Class registeree) {
            super(registeree);
            register(new MyHK2Binder());
            packages(true, "com.mycompany");
        }
    }
}

必须这样做实际上很好,因为您可以将 Binder 切换为测试绑定器,在其中您将服务绑定到模拟服务或其他东西。我在这里没有这样做,但这很容易做到:将 new MyHK2Binder() 调用中的 register() 替换为执行如下绑定的方法:

bind(MyTestServiceImpl.class).to(MyService.class).in(Singleton.class);

瞧。非常好。显然,您可以使用命名绑定获得类似的结果,但这非常有效,甚至可能更简单、更清晰。

希望这可以帮助某人节省我花费大量时间来完成这项工作。

于 2018-08-19T16:32:19.800 回答