@ManagedProperty的CDI替换 [英] CDI Replacement for @ManagedProperty
问题描述
我对CDI和JSF都很陌生,我正在尝试将Richfaces 4展示的一些代码转换为使用CDI而不是JSF注释。
I'm very new to both CDI and JSF, and I'm trying to convert some code from Richfaces 4 showcase to use CDI instead of JSF annotations.
我知道我可以使用@Named替换@MangedBean和@Inject来替换@ManagedProperty。但我遇到了一些麻烦。我正在尝试专门转换Richfaces Tree示例。
I understand that I can use @Named to replace @MangedBean and @Inject to replace @ManagedProperty. But I'm having some trouble. I'm trying to convert the Richfaces Tree example specifically.
我做了以下更改,我知道这不正确所以请不要使用此:
I have made the following changes and I know this is not correct so please don't use this:
//@ManagedBean
//@ViewScoped
@Named
@SessionScoped
public class TreeBean implements Serializable {
private static final long serialVersionUID = 1L;
// @ManagedProperty(value = "#{cdsParser.cdsList}")
// private List<CDXmlDescriptor> cdXmlDescriptors;
@Inject
private Instance<CDXmlDescriptor> cdXmlDescriptors;
// I also Tried :
// @Inject
// private CDParser cdsParser;
// private List<CDXmlDescriptor> cdXmlDescriptors = cdsParser.getCdsList();
........
然后我添加了(和我不确定这是否需要):
Then I added (and I'm not sure this is needed):
@Named
@SessionScoped
public class CDXmlDescriptor implements Serializable { ...
并更改:
//@ManagedBean(name = "cdsParser")
@Named("CDParser")
//@Named
@SessionScoped
public class CDParser implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3890828719623315368L;
@Named
private List<CDXmlDescriptor> cdsList;
我无法找出替换@ManagedProperty的正确方法(value =#{cdsParser.cdsList} )使用CDI?
I cannot figure out the proper way to replace @ManagedProperty(value = "#{cdsParser.cdsList}") using CDI?
推荐答案
因为你的 cdsList
没有bean类你需要一个生产者领域或制作方法使其可注射。
Since your cdsList
is no bean class you need a producer field or a producer method to make it injectable.
生产者字段示例:
import javax.enterprise.inject.Produces;
...
@Named
@Produces
private List<CDXmlDescriptor> cdsList;
生产者方法示例:
import javax.enterprise.inject.Produces;
private List <CDXmlDescriptor> cdsList;
...
@Named("cdsList")
@Produces
public List<CDXmlDescriptor> getCdsList {
return cdsList;
};
如果没有其他生产者字段或生产者方法返回相同的bean类型,则此方法有效。否则,您需要为生产者字段引入一个特殊限定符来解决歧义:
This works if there is no other producer field or producer method that returns the same bean type. Otherwise you need to introduce a special qualifier for your producer field to resolve ambiguity:
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface CdsList {
}
带
@Named @Produces @CdsList
private List<CDXmlDescriptor> cdsList;
这篇关于@ManagedProperty的CDI替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!