这是我第一次与GAE合作,我正试图让CDI正常工作。 长话短说:我的servlet中的@Inject
字段没有被注入(它总是null
)。
我正在使用Eclipse并在GAE SDK中包含的本地测试服务器上调试应用程序(该程序也由Eclipse启动)。当我在localhost
上访问servlet时,我得到someService
的空指针异常。我还输出someService
的值来验证它是否真的是null
。
编辑:当我向注入点添加@Named("skldjfx")
限定符时,Weld抱怨依赖性不满意(在验证阶段),这是一个好兆头。但是,该字段仍然始终为null
。
的Servlet
public class BlogServlet extends HttpServlet {
@Inject private SomeService someService;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello. " + someService.getSomeValue());
// \
// always null
}
}
SomeService
上课:
@ApplicationScoped
public class SomeService {
private String someValue = "some value";
public String getSomeValue() {
return someValue;
}
}
我在Listener
中配置了Weld web.xml
:
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
正确加载侦听器,因为它记录了初始化消息:org.jboss.weld.environment.servlet.Listener contextInitialized
。
我在beans.xml
和war/WEB-INF
中都包含(空)war/META-INF
。我还试过没有META-INF
。 可能beans.xml
未处理? WEB-INF
文件夹中的其他内容(例如web.xml
) 正确处理。
如何验证处理beans.xml
和/或修复SomeService
是否未注入?