我在本地计算机上安装了Websphere file (wlp 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>应用程序是一个简单的JSP页面,它调用bean方法。该方法使用@Inject bean:
package pkg1;
import javax.inject.Inject;
public class Test {
@Inject
private Bean1 bean1;
public String test() {
return bean1.testMethod();
}
}应用程序启动后,我在那里获得了NullPointerException。在我看来,CDI注入不起作用。有人能帮我吗?
发布于 2015-04-17 12:55:29
我在JSP代码中使用了@Inject,该代码在JSP中似乎不起作用。当我将所有注入移到Java并使用CDI BeanManager实例化第一个bean时,所有其他@注入都开始工作。
不太方便,但证明CDI是有效的。
发布于 2015-04-17 08:44:37
这个错误表明Test不是一个bean。如果要对类执行注入,类应该是Bean或JavaEE组件,详见EE7规范表EE.5-1。
测试类没有bean定义注释,所以它不是隐式bean存档中的bean (没有beans.xml,也没有bean.xml,但有bean-discovery=“注释”)。
要解决这个问题,您可以使用Bean定义注释(如依赖项、ApplicationScoped等)对ApplicationScoped进行注释。或者,添加一个空白的beans.xml或beans.xml ( bean发现模式=“all”),这有效地使测试成为一个具有依赖作用域的bean。
https://stackoverflow.com/questions/29676424
复制相似问题