CDI不能在简单的Websphere Liberty Profile设置中工作

时间:2015-04-16 13:35:49

标签: websphere-liberty

我在服务器上安装了以下server.xml文件的本地计算机上安装了Websphere Liberty Profile(wlp-beta-javaee7-2015.4.0.0.zip):

<?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-ed bean:

package pkg1;
import javax.inject.Inject;

public class Test {
    @Inject
    private Bean1 bean1;

    public String test() {
        return bean1.testMethod();
    }
}

应用程序启动后,我在那里得到NullPointerException。在我看来,CDI注射不起作用。 有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

错误表明Test不是bean。如果要对类执行注入,则该类应该是EE7规范的表EE.5-1中详述的Bean或JavaEE组件。

Test类没有bean定义注释,因此它不是隐式bean存档中的bean(没有beans.xml或者有bean.xml但是bean-discovery-mode =&#34;注释&#34;)。

要解决此问题,您可以使用Bean定义注释(例如Dependent,ApplicationScoped等)注释Test.java。或者,使用bean-discovery-mode =&#34;添加空白beans.xml或beans.xml。 all&#34;,它有效地使Test成为具有Dependent范围的bean。

答案 1 :(得分:0)

我在JSP代码中使用了 @Inject ,这似乎不适用于JSP。 当我将所有注入移动到Java并使用 CDI BeanManager 来实例化第一个bean时,所有其他的@Injects开始工作。

不太方便,但事实证明CDI正在发挥作用。

相关问题