wildfly 9 的构造函数注入不起作用
Constructor injection with wildfly 9 not working
我正在尝试在 wildfly 9.0.2 final 上部署一个简单的 WAR 文件。 WAR 目前没有 web.xml 也没有 EJB - 只是通过注释,一个 websocket 端点,在构造函数和工厂中进行依赖注入以产生依赖关系。在部署时,我收到错误:
java.lang.NoSuchMethodException: com.aip.textspace.infra.control.websocket.MainEndPoint.<init>
看起来 widlfly 正试图调用一个不存在的 null 构造函数。这是 MainEndPoint 的构造函数:
@Inject
public MainEndPoint(SessionController control) {
super();
this.control = control;
}
我有另一个名为 SessionControllerFactory 的 class,它有一个用于注入的生产者方法:
@Produces
public SessionController build() {
...
在 Maven POM 中,我引用了 CDI 库:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
那么我遗漏了什么是 wildfly 调用依赖注入构造函数所需要的?
顺便说一句,基于 related response,我尝试向 MainEndPoint 添加一个空构造函数。在那种情况下,部署成功但注入永远不会发生,并且在 MainEndPoint.this.control 中加入 this.control 时我得到空指针异常。
原因很明显:SessionController
还没有被注册为 CDI bean。
请确保在以下代码中:
@Produces
public SessionController build() {
...
}
注释 @Produces
指出 @javax.enterprise.inject.Produces
,而不是 @javax.ws.rs.Produces
意外。
另外请确保 SessionControllerFactory
包含在您的 war 存档中并且 它是一个托管 bean class.
顺便说一句:您没有提及任何关于您的 beans.xml
文件的信息(通常在 WildFly 9 和 EE 7 中是可选的)。出于测试目的,您可以添加此类文件,设置其属性 bean-discovery-mode="ALL"
并检查发生了什么。
我正在尝试在 wildfly 9.0.2 final 上部署一个简单的 WAR 文件。 WAR 目前没有 web.xml 也没有 EJB - 只是通过注释,一个 websocket 端点,在构造函数和工厂中进行依赖注入以产生依赖关系。在部署时,我收到错误:
java.lang.NoSuchMethodException: com.aip.textspace.infra.control.websocket.MainEndPoint.<init>
看起来 widlfly 正试图调用一个不存在的 null 构造函数。这是 MainEndPoint 的构造函数:
@Inject
public MainEndPoint(SessionController control) {
super();
this.control = control;
}
我有另一个名为 SessionControllerFactory 的 class,它有一个用于注入的生产者方法:
@Produces
public SessionController build() {
...
在 Maven POM 中,我引用了 CDI 库:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
那么我遗漏了什么是 wildfly 调用依赖注入构造函数所需要的?
顺便说一句,基于 related response,我尝试向 MainEndPoint 添加一个空构造函数。在那种情况下,部署成功但注入永远不会发生,并且在 MainEndPoint.this.control 中加入 this.control 时我得到空指针异常。
原因很明显:SessionController
还没有被注册为 CDI bean。
请确保在以下代码中:
@Produces
public SessionController build() {
...
}
注释 @Produces
指出 @javax.enterprise.inject.Produces
,而不是 @javax.ws.rs.Produces
意外。
另外请确保 SessionControllerFactory
包含在您的 war 存档中并且 它是一个托管 bean class.
顺便说一句:您没有提及任何关于您的 beans.xml
文件的信息(通常在 WildFly 9 和 EE 7 中是可选的)。出于测试目的,您可以添加此类文件,设置其属性 bean-discovery-mode="ALL"
并检查发生了什么。