Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Weld with Dropwizard

I am trying to use Weld-SE for dependency injection in a dropwizard application. I can bootstrap Weld and inject in the Application class like so:

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();
  }
}

I have written a producer method in a separate class for the RestResource and this is also injected fine. However in the resource class the service is not injected:

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

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

Here service is always null. Does anyone know how to make this work?

like image 983
Martin Charlesworth Avatar asked Nov 04 '14 23:11

Martin Charlesworth


2 Answers

Dropwizard is using Jersey whose dependency injection is based on HK2 and not CDI. As a consequence you need to have a bridge between the two. This is what the jersey-gf-cdi is for:

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

You only need to have that JAR in the classpath. You can see here a configuration for Jetty here: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/pom.xml

And hereafter an example of CDI bean injection into JAX-RS resource: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/src/main/java/io/astefanutti/cdeye/web/BeansResource.java

like image 103
Antonin Stefanutti Avatar answered Nov 02 '22 07:11

Antonin Stefanutti


For DropWizard 0.8.1 and Weld 2.2 the procedure is as follows:

1) Add dependencies to 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) Add beans.xml file to src/main/resources/META-INF and add an inclusion filter for application packages. This is especially needed when using the shaded jar - without the filter Weld would scan every class in the shaded 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) Register Weld's listener in your application class

@Override
public void run(Configuration conf, Environment env) throws Exception {
    env.servlets().addServletListeners(new org.jboss.weld.environment.servlet.Listener());
}
like image 33
jozefhartinger Avatar answered Nov 02 '22 08:11

jozefhartinger