I am playing around with Weld CDI configuration. I got stuck with NULL being returned. Theoritically my configuration should make proper object instance created but I can't find reason it doesn't...
I looked up for similar questions, but most issues were related to use of new keyword while instantating the object. The other advise is to use application server. I'm running wildfly, so it's not the case as well.
Log:
Feb 17, 2021 12:12:18 AM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 4.0.0 (Final)
Feb 17, 2021 12:12:19 AM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Feb 17, 2021 12:12:19 AM org.jboss.weld.environment.se.WeldContainer fireContainerInitializedEvent
INFO: WELD-ENV-002003: Weld SE container 75c04868-bd06-4acb-874c-db603ada27b0 initialized
Exception in thread "main" java.lang.NullPointerException
at MessagePrinter.printMessage(MessagePrinter.java:9)
at Main.main(Main.java:10)
Weld SE container 75c04868-bd06-4acb-874c-db603ada27b0 shut down by shutdown hook
Process finished with exit code 1
Here's my setup:
beans.xml simple as can be
<?xml version="1.0"?>
<beans bean-discovery-mode="all" version="1.1"
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"/>
Main class - creating Weld container
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class Main {
public static void main(String[] args) {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
MessagePrinter printer = container.select(MessagePrinter.class).get();
printer.printMessage();
weld.shutdown();
}
}
MessagePrinter class
import javax.inject.Inject;
public class MessagePrinter {
@Inject
private MessageProducer messageProducer;
public void printMessage() {
String message = messageProducer.getMessage();
System.out.println(message);
}
}
So above MessageProducer variable is always null although one only existing MessageProducer implementation (mark as @default) should be injected...
MessageProducer interface
public interface MessageProducer {
public String getMessage();
}
SimpleMessageProducer class (MessageProducer implementation)
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
@Default
@Any
public class SimpleMessageProducer implements MessageProducer {
@Override
public String getMessage() {
return "Example message " + System.currentTimeMillis();
}
}
Project structure:
-src
--java
---FileMessage
---FileMessageProducer
---Main
---MessagePrinter
---MessageProducer
---SimpleMessageProducer
-resources
--META-INF
---beans.xml
As you are using Weld 4.0.0.Final, you should use jakarta.inject namespace instead of javax.inject.
You probably are having issues due overlapping classpaths. Keep only the org.jboss.weld.se:weld-se-core:4.0.0.Final dependency and you will probably be fine.