How to setup SparkJava CDI with DeltaSpike and Weld

501 Views Asked by At

So I've started setting up a simple SparkJava application from scratch with the basic goal of comparing it to a mirrored SpringBoot (with SpringData) application which I've already built. However I've been struggling with DeltaSpike (which is the alternative to SpringData chosen) setup as that adds a layer of CDI complexity which I didn't have to handle within SpringBoot.

Long story short after trying out some (probably wrong) approaches: now I'm stuck at enabling Weld (CDI) in the SparkJava Jetty container. I've taken a look on how to enable Weld for Jetty but that only got me more confused - and I'm not even sure that it's supposed to work.

What I'm getting now is: Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/weld/events/WeldEvent.

Since there are many files it's better to just share my github repo: https://github.com/vitorbmiranda/taskapp-sparkjava. Don't mind unused code and stuff like that as it's still a WIP.

A final disclaimer: apart from that error itself I feel like I may be completely missing something or my approach with SparkJava/CDI/DeltaSpike could be completely off, or my Maven dependencies could not make any sense, so any directions on that would also be appreciated. Cheers.


Update Sep 2nd: did a fresh setup on a new repo. Managed to customize the Spark embedded Jetty handler and added the Weld Listener to it.

This is how I've added the listeners:

import org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener;
import org.jboss.weld.environment.servlet.Listener;
...
JettyHandler handler = new JettyHandler(matcherFilter);
...
handler.addEventListener(new BeanManagerResourceBindingListener());
handler.addEventListener(new Listener());

This is from the startup log (using TRACE level within logback.xml)

5:46:50.427 [Thread-0] DEBUG o.e.j.u.component.ContainerLifeCycle - spark.embeddedserver.jetty.JettyHandler1530180168==dftMaxIdleSec=-1 added {org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener@49483272,UNMANAGED} 15:46:50.432 [Thread-0] DEBUG o.e.j.u.component.ContainerLifeCycle - spark.embeddedserver.jetty.JettyHandler1530180168==dftMaxIdleSec=-1 added {org.jboss.weld.environment.servlet.Listener@62383f62,UNMANAGED}

No "Weld" references after that. If I try to use any @Injected bean it won't work as expected (since logs didn't give me anything related to Bean/classpath lookup):

public class TestController {

  @Inject
  TestService testService;

  public static String getTest(Request request, Response response) {
    // NPE here
    testService.test();
    return value;
  }

}
@Default
public class TestService {

  public String test() {
    return "123";
  }

}

My pom.xml looks like:

    <dependency>
      <groupId>com.sparkjava</groupId>
      <artifactId>spark-core</artifactId>
      <version>2.9.1</version>
    </dependency>

    <dependency>
      <groupId>org.jboss.weld.se</groupId>
      <artifactId>weld-se-core</artifactId>
      <version>3.1.2.Final</version>
    </dependency>

    <dependency>
      <groupId>org.jboss.weld.servlet</groupId>
      <artifactId>weld-servlet-core</artifactId>
      <version>3.1.2.Final</version>
    </dependency>

    <dependency>
      <groupId>org.jboss.weld</groupId>
      <artifactId>weld-spi</artifactId>
      <version>3.1.SP1</version>
    </dependency>

The META-INF/beans.xml (which I would think it's not necessary, anyway) is:

<?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"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
       http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  bean-discovery-mode="all">
</beans>
0

There are 0 best solutions below