【问题标题】:HK2 equivalent of @Provides in Guice for Jersey 2HK2 相当于 @Provides in Guice for Jersey 2
【发布时间】:2014-04-04 08:04:54
【问题描述】:

我一直在使用 Jersey 1.X 和 Google Guice 进行依赖注入。切换到 Jersey 2.X 似乎意味着您需要使用 HK2 进行依赖注入,我正在努力寻找我在 Guice 中拥有的一些东西。

在带有 Guice 的 Jersey 1.X 中,我会为应用程序提供类似的东西:

public class GuiceServletTestConfig extends GuiceServletContextListener  {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule(){
            @Override
            protected  void configureServlets(){
                bind(MyResource.class);
                serve("/*").with(GuiceContainer.class);
                bind(MyDAO.class).to(MyDAOSQL.class)
            }
        });
    }
}

还有这样的测试:

public class GuiceServletTestConfig extends GuiceServletContextListener  {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule(){
            @Override
            protected  void configureServlets(){
                bind(MyResource.class);
                serve("/*").with(GuiceContainer.class);
            }

            @Provides
            MyDAO provideMockMyDAO(){
                MyDAO dao = mock(MyDAO.class);
                return dao;
            }
        });
    }
}

我的任何资源都应该是这样的:

@Path("myresource")
public class MyResource {
    private MyDAO myDAO;

    @Inject
    protected void setMyDAO(MyDAO myDAO) {
        this.myDAO = myDAO;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        // Do something with myDAO
        // Return response    
    }
}

那是我可以为我的测试定义模拟,一切都很好。

但是,对于 Jersey 2.X,我找不到 @Provides 注释的任何等效项。 MyResource 实际上是相同的。对于真实应用程序的依赖注入,我有:

public class Application extends ResourceConfig {
    public Application() {
        packages("com.my.package.resources");

        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(MyDAOSQL.class).to(MyDAO.class);
            }
        });
    }
}

但我不知道如何为测试提供模拟。有人知道怎么做吗?

【问题讨论】:

  • 如果您想继续使用 Guice,请考虑切换到 Resteasy。它是 JAX-RS 2 的实现,并且完全兼容 Guice。

标签: java dependency-injection jersey guice hk2


【解决方案1】:

HK2 允许你绑定像@Provides 一样工作的工厂。这是javadoc。我确实认为这不太方便,因为您必须创建一个实现 Factory 的类。我可能会添加增强 Jira 来执行 CDI 样式 @Produces。

此外,您可以通过Guice-HK2 bridge 继续在泽西岛使用 Guice(很多人都这样做)。使用 Bridge 时有一些限制(例如,必须对 Guice 创建的类使用 @HK2Inject 但要注入 HK2 服务),但大多数事情仍然有效。

【讨论】:

    【解决方案2】:

    好的,所以我想出了一个适合我的方法。让我失望的一件事是将 bind().to() 从 Guice 换成 HK2。在 Guice,你写:

    bind(Abstract.class).to(Concrete.class)
    

    在 HK2 中,你写:

    bind(Concrete.class).to(Abstract.class)
    

    获取provides行为的方式可以通过以下代码实现:

    public class MyResourceIT extends JerseyTest {
        @Override
        protected Application configure() {
            ResourceConfig resourceConfig = new ResourceConfig();
            resourceConfig.register(MyResource.class);
    
            resourceConfig.register(new AbstractBinder() {
                @Override
                protected void configure() {
                    bind(provideMyDaoMock()).to(MyDao.class);
                }
    
                private MyDao provideMyDaoMock() {
                    MyDao myDaoMock = mock(MyDao.class);
                    return myDaoMock;
                }
            });
            return resourceConfig;
        }
    }
    

    【讨论】:

    • 是的,我们确实切换了它们,所以现在你可以像 bind(Concrete.class).to(InterfaceA.class).to(InterfaceB.class) 一样进行操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2016-04-11
    • 2016-10-14
    • 2015-07-13
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多