@FacesComponent中的@Inject-ed值为null [英] @Inject-ed value null in @FacesComponent

查看:300
本文介绍了@FacesComponent中的@Inject-ed值为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是CDI不支持具有 @javax.faces.component.FacesComponent 的类。这是真的?

I have the impression that CDI is not working with classes that have a @javax.faces.component.FacesComponent. Is this true?

这是我的例子,不起作用。 MyInjectableClass 用于代码中注入不成问题的其他位置,因此它必须是 @FacesComponent 我认为是注释。

Here's my example, that doesn't work. The MyInjectableClass is used at other points in the code where injection is not a problem, so it must be about the @FacesComponent annotation I think.

我要注入的课程:

@Named
@Stateful
public class MyInjectableClass implements Serializable {

    private static final long serialVersionUID = 4556482219775071397L;
}

使用该类的组件;

@FacesComponent(value = "mycomponents.mytag")
public class MyComponent extends UIComponentBase implements Serializable {

    private static final long serialVersionUID = -5656806814384095309L;

    @Inject
    protected MyInjectableClass injectedInstance;


    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        /* injectedInstance is null here */
    }
}


推荐答案

不幸的是,即使对于JSF 2.2 @FacesComponent @FacesValidator @FacesConverter 不是有效的注射目标(读取< a href =http://jdevelopment.nl/jsf-22/ =nofollow noreferrer>在JSF 2.2中有什么新功能??由Arjan Tijms提供更多细节)。正如Arjan所指出的那样:

Unfortunately, even for JSF 2.2 @FacesComponent, @FacesValidator and @FacesConverter are not valid injection targets (read What's new in JSF 2.2? by Arjan Tijms for more details). As Arjan points out:


尽管如此,JSF 2.3可能会考虑这些因素。

It’s likely that those will be taken into consideration for JSF 2.3 though.

你现在可以做些什么?好吧,你基本上有两个选择:

What can you do for now? Well, you've got basically two choices:


  1. 处理通过查找进行CDI注入,或切换到EJB并执行更简单的EJB查找;

  2. 使用 @Named 而不是 @FacesComponent @Inject 组件的方式与在faces-config.xml中注册您的组件。由于UI组件实例是通过JSF Application#createComponent()创建的,而不是通过CDI创建的,因此您也需要一个自定义的应用程序实现(就像OmniFaces对那些转换器/验证器一样)。

  1. Handle CDI injection via lookup, or switch to EJB and do the simpler EJB lookup;
  2. Annotate tour class with @Named instead of @FacesComponent, @Inject the component the way you did and register your component in faces-config.xml. As the UI component instance is created via JSF Application#createComponent(), not via CDI you will also need a custom Application implementation as well (exactly like OmniFaces has for those converters/validators).

顺便说一下,你有两个问题就是你到目前为止所得到的:(1)的意思@Named @状态当前者来自CDI世界而后者来自EJB世界时(2)你确定你打算将状态保存在基本上每次请求都重新创建的faces组件中吗?

And, by the way, you've got two issues with what you've got this far: (1) what is meant by @Named @Stateful when the former is from a CDI world and the latter is from EJB world and (2) are you sure you intend to keep state in a faces component that's basically recreated on every request?

这篇关于@FacesComponent中的@Inject-ed值为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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