Question

How can I start an application with CDI that uses Weld within a normal Java SE environment, that is simply started from the command line and has no Glassfish or Tomcat in the background? With Weld 1.x you could do something like

public static void main(String[] args) {
   final WeldContainer weld = new Weld().initialize();
   service = weld.instance().select(MyService.class).get();
}

But in Weld 2.x the initialize() is missing.

I can do something like this:

@ApplicationScoped
public class Main {
   @Inject
   Foo foo;

   public void main(@Observes ContainerInitialized event) {
       System.out.println("Starting application");
   }
}

This will inject Foo, but I want to pack my whole Application into a jar to be used by an other application that does not need to wait for Weld to actually start running. So I basically want a way to initialize Weld properly.

Was it helpful?

Solution

Bootstrapping Weld SE has not changed between Weld 1.x and 2.x, see the reference manual.

Just make sure to import org.jboss.weld.environment.se.Weld, not the new class org.jboss.weld.Weld.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top