"how to inject @ejb, @persistencecontext, @inject, @autowired, etc in @facesconverter?" Code Answer

4

can i use @ejb to inject my service into a @facesconverter?

no, not until jsf 2.3 is released. the jsf/cdi guys are working on that for jsf 2.3. see also jsf spec issue 1349 and this related "what's new in jsf 2.3?" article of my fellow arjan tijms. only then dependency injection like @ejb, @persistencecontext, @inject, etc will work in a @facesconverter when you explicitly add managed=true attribute to the annotation.

@facesconverter(value="yourconverter", managed=true)
public class yourconverter implements converter {

    @inject
    private yourservice service;
    // ...
}

if not, what's the "correct" way to do this?

before jsf 2.3, you have several options:

  1. make it a managed bean instead. you can make it a jsf, cdi or spring managed bean via @managedbean, @named or @component. the below example makes it a jsf managed bean.

    @managedbean
    @requestscoped
    public class yourconverter implements converter {
    
        @ejb
        private yourservice service;
        // ...
    }
    

    and the below example makes it a cdi managed bean.

    @named
    @requestscoped
    public class yourconverter implements converter {
    
        @inject
        private yourservice service;
        // ...
    }
    

    reference it as <h:inputxxx converter="#{yourconverter}"> instead of <h:inputxxx converter="yourconverter">, or as <f:converter binding="#{yourconverter}"> instead of <f:converter converterid="yourconverter">. don't forget to remove the @facesconverter annotation!

    the disadvantage is that you cannot specify forclass and thus need to manually define the converter everywhere in the view where necessary.

  2. inject it in a regular managed bean instead.

    @managedbean
    @requestscoped
    public class yourbean {
    
        @ejb
        private yourservice service;
        // ...
    }
    

    and in your converter, grab or call it via el.

    yourbean yourbean = context.getapplication().evaluateexpressionget(context, "#{yourbean}", yourbean.class);
    
    // then e.g. either
    yourentity yourentity = yourbean.getservice().findbystringid(value);
    // or
    yourentity yourentity = yourbean.findentitybystringid(value);
    

    this way you can keep using @facesconverter.

  3. manually grab the ejb from jndi.

    yourservice yourservice = (yourservice) new initialcontext().lookup("java:global/appname/yourservice");
    

    the disadvantage is that there is a certain risk that this is not entirely portable. see also inject ejb bean from jsf managed bean programmatically.

  4. install omnifaces. since version 1.6, it transparently adds support for @ejb (and @inject) in a @facesconverter without any further modification. see also the showcase. if you happen to need the converter for <f:selectitem(s)>, then the alternative is to use its selectitemsconverter which will automatically perform the conversion job based on select items without the need for any database interaction.

    <h:selectonemenu ... converter="omnifaces.selectitemsconverter">
    

    see also conversion error setting value for 'null converter'.

see also:

  • how to inject in @facesvalidator with @ejb, @persistencecontext, @inject, @autowired
  • cdi injection into a facesconverter
  • getting an @ejb in a @facesvalidator and @facesconverter
By telcoM on June 16 2022

Answers related to “how to inject @ejb, @persistencecontext, @inject, @autowired, etc in @facesconverter?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.