【问题标题】:Using Weld with Dropwizard将焊接与 Dropwizard 一起使用
【发布时间】:2015-01-01 01:04:36
【问题描述】:

我正在尝试在 dropwizard 应用程序中使用 Weld-SE 进行依赖注入。我可以像这样引导 Weld 并注入 Application 类:

public class App extends Application<AppConfig> {

  @Inject NameService service;
  @Inject RestResource resource;

  public static void main(String[] args) throws Exception {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    App app = container.instance().select(App.class).get();     
    app.run(args);
    weld.shutdown();
  }
}

我在一个单独的类中为 RestResource 编写了一个生产者方法,并且注入也很好。但是在资源类中,服务没有被注入:

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public class RestResource {
    @Inject NameService service;

    @GET
    public String test() {
        return service.getName();
    }
}

这里的服务总是空的。有谁知道如何进行这项工作?

【问题讨论】:

  • 你用的是什么容器?
  • 没有容器。 Dropwizard 是 java se,它构建了一个包含所有服务的胖 jar(默认情况下,jetty、jersey 等都在其中)。我正在尝试做的是将weld-se 作为其中之一。

标签: java cdi dropwizard weld


【解决方案1】:

对于 DropWizard 0.8.1 和 Weld 2.2,过程如下:

1) 给 pom.xml 添加依赖:

<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-core</artifactId>
    <version>2.2.11.Final</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.ext.cdi</groupId>
    <artifactId>jersey-cdi1x</artifactId>
    <version>2.17</version>
</dependency>
<!-- the following additional dependencies are needed by weld -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

2) 将 beans.xml 文件添加到 src/main/resources/META-INF 并为应用程序包添加包含过滤器。这在使用阴影 jar 时尤其需要 - 如果没有过滤器 Weld 将扫描阴影 jar 中的每个类。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:weld="http://jboss.org/schema/weld/beans">

    <weld:scan>
        <weld:include name="com.example.**" />
    </weld:scan>
</beans>

3) 在你的应用类中注册 Weld 的监听器

@Override
public void run(Configuration conf, Environment env) throws Exception {
    env.servlets().addServletListeners(new org.jboss.weld.environment.servlet.Listener());
}

【讨论】:

  • 这导致java.lang.NoClassDefFoundError: javax/servlet/jsp/JspFactory at org.jboss.weld.environment.servlet.WeldServletLifecycle.initialize(WeldServletLifecycle.java:174) ~[weld-servlet-core-2.2.11.Final.jar:2015-04-15 09:30]
【解决方案2】:

Dropwizard 正在使用 Jersey,其依赖注入基于 HK2 而不是 CDI。因此,您需要在两者之间架起一座桥梁。这就是jersey-gf-cdi 的用途:

<dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi</artifactId>
</dependency>

您只需要在类路径中包含该 JAR。您可以在此处查看 Jetty 的配置: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/pom.xml

以下是 CDI bean 注入 JAX-RS 资源的示例: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/src/main/java/io/astefanutti/cdeye/web/BeansResource.java

【讨论】:

  • 谢谢,但我认为这行不通 - 该模块似乎只存在于 jersey 2+,但当前的 dropwizard (0.7.0) 使用 jersey 1.18。
  • @martincharlesworth 如果你愿意可以试试 Dropwizard 0.8.0 RC,它支持 Jersey 2.13
  • 感谢 antonin 和 abdullah - 使用那个 jar 和 dropwizard 0.8.0-snapshot 它可以工作
猜你喜欢
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
相关资源
最近更新 更多