在Google App Engine上焊接CDI:servlet中的注入没有发生 [英] Weld CDI on Google App Engine: Injection in servlet not happening

查看:109
本文介绍了在Google App Engine上焊接CDI:servlet中的注入没有发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次和GAE合作,我正在努力让CDI工作。长话短说:我的servlet中的 @Inject 字段没有被注入(它总是 null )。

我正在Eclipse中调试应用程序,并在GAE SDK中包含的本地测试服务器(也由Eclipse启动)上进行调试。当我访问 localhost 上的servlet时,我得到 someService 的空指针异常。我还输出了 someService 的值来验证它真的是 null ,它是。



编辑:当我向注入点添加 @Named(skldjfx) Weld抱怨说依赖不满意(在验证阶段),所以这是一个好兆头。但是,该字段仍然是 null



Servlet:

  public class BlogServlet扩展HttpServlet {

@Inject private SomeService someService;

public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws IOException {

resp.setContentType(text / plain);
resp.getWriter()。println(Hello。+ someService.getSomeValue());
// \
//总为空
}
}

SomeService class:

  @ApplicationScoped 
public class SomeService {

private String someValue =some value;

public String getSomeValue(){
return someValue;




$ b我已经配置了Weld的 Listener web.xml中

  <听者GT; 
< 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 未注入?


<解决方案

看起来像使用GAE的CDI是不可能的,因为GAE的Jetty分支会忽略 jetty-web.xml ,它需要指定 BeanManager 。请参阅此链接此链接。真正奇怪的GAE不支持使用CDI。



请注意,这些链接真的很旧,但到目前为止我还没有发现任何相反的证据。



无论如何,我的下一步将是使用Google自己的依赖注入框架Guice。在GAE中使用它在此处中描述。不过我更喜欢CDI。


This is my first time working with GAE and I'm trying to get CDI working. Long story short: The @Inject field in my servlet is not getting injected (it's always null).

I'm working in Eclipse and debug the application on the local test server included in the GAE SDK (which is started by Eclipse as well). When I access the servlet on localhost, I get a null-pointer exception for someService. I've also output the value of someService to verify it is really null, which it is.

Edit: When I added a @Named("skldjfx") qualifier to the injection point, Weld complained the dependency is unsatisfied (in the validation phase), so that's a good sign. However, the field is still always 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 class:

@ApplicationScoped
public class SomeService {

    private String someValue = "some value";

    public String getSomeValue() {
        return someValue;
    }
}

I've configured Weld's Listener in web.xml:

<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

The listener is properly loaded because it logs its initialization message: org.jboss.weld.environment.servlet.Listener contextInitialized.

I've included (an empty) beans.xml in both war/WEB-INF and war/META-INF. I also tried it without META-INF. Maybe beans.xml isn't processed? Other contents in the WEB-INF folder (such as web.xml) are processed properly.

How can I verify if beans.xml is processed and/or fix SomeService not getting injected?

解决方案

It looks like it's not possible to use CDI with GAE, because GAE's Jetty fork ignores jetty-web.xml, which is needed to specify the BeanManager. See this link and this link. Really strange GAE is not supporting the use of CDI.

Note that these links are really old, but so far I haven't found any evidence to the contrary.

Anyway, my next step will be to use Google's own dependency injection framework Guice. Using it with GAE is described here. I'd prefer CDI though.

这篇关于在Google App Engine上焊接CDI:servlet中的注入没有发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆