CDI Replacement for @ManagedProperty

I am very new to CDI and JSF, and I am trying to convert some code from the Richfaces 4 showcase to use CDI instead of JSF annotations.

I understand that I can use @Named to replace @MangedBean and @Inject to replace @ManagedProperty. But I have a problem. I am trying to convert an example Richfaces tree specifically.

I made the following changes, and I know this is wrong, so do not 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 if this is necessary):

 @Named @SessionScoped public class CDXmlDescriptor implements Serializable { ... 

and changed:

 //@ManagedBean(name = "cdsParser") @Named("CDParser") //@Named @SessionScoped public class CDParser implements Serializable{ /** * */ private static final long serialVersionUID = 3890828719623315368L; @Named private List<CDXmlDescriptor> cdsList; 

I canโ€™t find the right way to replace @ManagedProperty (value = "# {cdsParser.cdsList}") using CDI?

+6
source share
1 answer

Since your cdsList not a bean class, you need a producer field or producer to make it injectable.

An example for a manufacturer field:

 import javax.enterprise.inject.Produces; ... @Named @Produces private List<CDXmlDescriptor> cdsList; 

Example for manufacturer method:

 import javax.enterprise.inject.Produces; private List <CDXmlDescriptor> cdsList; ... @Named("cdsList") @Produces public List<CDXmlDescriptor> getCdsList { return cdsList; }; 

This works if there is no other producer field or producer method that returns the same bean type. Otherwise, you need to enter a special classifier for the field of your manufacturer in order to eliminate the 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 { } 

from

 @Named @Produces @CdsList private List<CDXmlDescriptor> cdsList; 
+5
source

Source: https://habr.com/ru/post/911991/


All Articles