Using Weld with Dropwizard
Asked Answered
I

2

6

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?

Instep answered 4/11, 2014 at 23:27 Comment(2)
What container are you using?Golden
There is no container. Dropwizard is java se, it builds a fat jar with all the services (jetty, jersey etc are in there by default) embedded. What I'm trying to do is include weld-se as one of those.Instep
F
9

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

Fritzsche answered 5/11, 2014 at 12:4 Comment(3)
Thanks but I don''t think this can work - that module appears to only exist for jersey 2+ but the current dropwizard (0.7.0) is using jersey 1.18.Instep
@martincharlesworth If you want you can try Dropwizard 0.8.0 RC, it supports Jersey 2.13Melnick
thanks antonin & abdullah - using that jar plus dropwizard 0.8.0-snapshot it worksInstep
S
2

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());
}
Symphonia answered 4/5, 2015 at 13:36 Comment(1)
This results in 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]Toga

© 2022 - 2024 — McMap. All rights reserved.