Using Weld with Dropwizard

I am trying to use Weld-SE to inject dependencies in a dropwizard application. I can load Weld and inject into the Application class like this:

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 wrote a producer method in a separate class for RestResource, and it is also perfectly nested. However, the service is not introduced in the resource class:

 @Path("/test") @Produces(MediaType.APPLICATION_JSON) public class RestResource { @Inject NameService service; @GET public String test() { return service.getName(); } } 

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

+6
source share
2 answers

Dropwizard uses a Jersey whose dependency injection is based on HK2 rather than CDI. As a result, you should have a bridge between them. This is what jersey-gf-cdi for:

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

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

And here is an example of embedding a CDI bean in a JAX-RS resource: https://github.com/astefanutti/cdeye/blob/cd6d31203bdd17262aab12d992e2a730c4f8fdbd/webapp/src/main/java/io/astefanutti/cdee/esjesource

+7
source

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

1) Add the 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 the beans.xml file to src / main / resources / META-INF and add an inclusion filter for application packages. This is especially necessary when using a shaded can - without a filter, Weld scans each class in a shaded can.

 <?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 listener in application class

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

Source: https://habr.com/ru/post/977733/


All Articles