CDI 在简单的 Websphere Liberty Profile 设置中不起作用
CDI doen't work in a simple Websphere Liberty Profile setup
我在本地计算机上安装了 Websphere Liberty Profile (wlp-beta-javaee7-2015.4.0.0.zip),服务器上有以下 server.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>jsp-2.3</feature>
<feature>cdi-1.2</feature>
<feature>ejbLite-3.2</feature>
<feature>jaxrs-2.0</feature>
<feature>jpa-2.1</feature>
<feature>jaxrs-2.0</feature>
<feature>jaxrsClient-2.0</feature>
<feature>concurrent-1.0</feature>
<feature>jndi-1.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" />
<applicationMonitor updateTrigger="mbean" />
<application id="test2_ear_exploded" location="C:\myfolder\test2\out\artifacts\test2_ear_exploded" name="test2_ear_exploded" type="ear" />
</server>
该应用程序是一个调用 bean 方法的普通 JSP 页面。该方法使用 @Inject-ed bean:
package pkg1;
import javax.inject.Inject;
public class Test {
@Inject
private Bean1 bean1;
public String test() {
return bean1.testMethod();
}
}
应用程序启动后,出现 NullPointerException。在我看来,CDI 注入不起作用。
有人可以帮我吗?
错误表明 Test 不是一个 bean。如果要在 class 上执行注入,class 应该是 Bean 或 JavaEE 组件,详见 Table EE7 规范的 EE.5-1。
测试 class 没有 bean 定义注释,因此它不是隐式 bean 存档中的 bean(没有 beans.xml 或具有 bean.xml 但 bean-discovery-mode= "annotated")。
要解决此问题,您可以使用 Bean 定义注释(例如 Dependent、ApplicationScoped 等)对 Test.java 进行注释。或者,使用 bean- 添加空白 beans.xml 或 beans.xml discovery-mode="all",它有效地将 Test 作为具有依赖范围的 bean。
我在 JSP 代码中使用了 @Inject,但在 JSP 中似乎不起作用。
当我将所有注入移至 Java 并使用 CDI BeanManager 实例化第一个 bean 时,所有其他 @Injects 开始工作。
不是很方便,但证明CDI是可以的。
我在本地计算机上安装了 Websphere Liberty Profile (wlp-beta-javaee7-2015.4.0.0.zip),服务器上有以下 server.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>jsp-2.3</feature>
<feature>cdi-1.2</feature>
<feature>ejbLite-3.2</feature>
<feature>jaxrs-2.0</feature>
<feature>jpa-2.1</feature>
<feature>jaxrs-2.0</feature>
<feature>jaxrsClient-2.0</feature>
<feature>concurrent-1.0</feature>
<feature>jndi-1.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" />
<applicationMonitor updateTrigger="mbean" />
<application id="test2_ear_exploded" location="C:\myfolder\test2\out\artifacts\test2_ear_exploded" name="test2_ear_exploded" type="ear" />
</server>
该应用程序是一个调用 bean 方法的普通 JSP 页面。该方法使用 @Inject-ed bean:
package pkg1;
import javax.inject.Inject;
public class Test {
@Inject
private Bean1 bean1;
public String test() {
return bean1.testMethod();
}
}
应用程序启动后,出现 NullPointerException。在我看来,CDI 注入不起作用。 有人可以帮我吗?
错误表明 Test 不是一个 bean。如果要在 class 上执行注入,class 应该是 Bean 或 JavaEE 组件,详见 Table EE7 规范的 EE.5-1。
测试 class 没有 bean 定义注释,因此它不是隐式 bean 存档中的 bean(没有 beans.xml 或具有 bean.xml 但 bean-discovery-mode= "annotated")。
要解决此问题,您可以使用 Bean 定义注释(例如 Dependent、ApplicationScoped 等)对 Test.java 进行注释。或者,使用 bean- 添加空白 beans.xml 或 beans.xml discovery-mode="all",它有效地将 Test 作为具有依赖范围的 bean。
我在 JSP 代码中使用了 @Inject,但在 JSP 中似乎不起作用。 当我将所有注入移至 Java 并使用 CDI BeanManager 实例化第一个 bean 时,所有其他 @Injects 开始工作。
不是很方便,但证明CDI是可以的。