焊接容器没有注入正确的对象,它 returns NULL
Weld container doesn't inject proper object, it returns NULL
我正在尝试 Weld CDI 配置。我被返回 NULL 卡住了。理论上我的配置应该创建正确的对象实例,但我找不到它不创建的原因...
我查找了类似的问题,但大多数问题都与在实例化对象时使用 new 关键字有关。另一个建议是使用应用程序服务器。我是运行野蝇,所以也不是这样
日志:
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
这是我的设置:
beans.xml尽可能简单
<?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"/>
主要class - 创建焊接容器
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);
}
}
所以上面的 MessageProducer 变量总是空的,尽管只有一个现有的 MessageProducer 实现(标记为@default)应该被注入...
MessageProducer 接口
public interface MessageProducer {
public String getMessage();
}
SimpleMessageProducer class(MessageProducer 实现)
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();
}
}
项目结构:
-src
--java
---FileMessage
---FileMessageProducer
---Main
---MessagePrinter
---MessageProducer
---SimpleMessageProducer
-resources
--META-INF
---beans.xml
由于您使用的是 Weld 4.0.0.Final,您应该使用 jakarta.inject 命名空间而不是 javax.inject。
您可能遇到了类路径重叠的问题。只保留 org.jboss.weld.se:weld-se-core:4.0.0.Final 依赖项,你可能会没事的。
我正在尝试 Weld CDI 配置。我被返回 NULL 卡住了。理论上我的配置应该创建正确的对象实例,但我找不到它不创建的原因...
我查找了类似的问题,但大多数问题都与在实例化对象时使用 new 关键字有关。另一个建议是使用应用程序服务器。我是运行野蝇,所以也不是这样
日志:
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
这是我的设置:
beans.xml尽可能简单
<?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"/>
主要class - 创建焊接容器
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);
}
}
所以上面的 MessageProducer 变量总是空的,尽管只有一个现有的 MessageProducer 实现(标记为@default)应该被注入...
MessageProducer 接口
public interface MessageProducer {
public String getMessage();
}
SimpleMessageProducer class(MessageProducer 实现)
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();
}
}
项目结构:
-src
--java
---FileMessage
---FileMessageProducer
---Main
---MessagePrinter
---MessageProducer
---SimpleMessageProducer
-resources
--META-INF
---beans.xml
由于您使用的是 Weld 4.0.0.Final,您应该使用 jakarta.inject 命名空间而不是 javax.inject。
您可能遇到了类路径重叠的问题。只保留 org.jboss.weld.se:weld-se-core:4.0.0.Final 依赖项,你可能会没事的。