How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?
Asked Answered
A

5

28

How can I inject a dependency like @EJB, @PersistenceContext, @Inject, @AutoWired, etc in a @FacesConverter? In my specific case I need to inject an EJB via @EJB:

@FacesConverter
public class MyConverter implements Converter {

  @EJB
  protected MyService myService;    

  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
    // myService.doSomething
  }

}

However, it didn't get injected and it remains null, resulting in NPEs. It seems that @PersistenceContext and @Inject also doesn't work.

How do I inject a service dependency in my converter so that I can access the DB?

Amundsen answered 5/10, 2011 at 18:8 Comment(0)
I
54

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:

Insufficiency answered 5/10, 2011 at 18:16 Comment(6)
ejb lookup should be portable no?Abbevillian
@Kalpesh: depends on how you package your EJBs and on the appserver make/version.Insufficiency
@Insufficiency I know this question is old, but is it "bad" to retrieve the UISelectItems from the UIComponent, then iterate over this select item list and find the value? I mean you need any injection or send request to the database (if the question isnt clear i can start a new question with an example)Vulcan
@OuerghiYassine: that converter exist already: showcase.omnifaces.org/converters/SelectItemsConverterInsufficiency
JSF 2.3 was released this week! Yay! javaserverfaces.java.net/2.3/download.htmlNaturalist
Tried Glassfish5.1 that contains JSF 2.3. NOT working. The sessionBean in facesConverter is null, not injected.Syncope
S
2

The answer is Yes if you can accommodate Seam Faces module in your web application. Please check this post Injection of EntityManager or CDI Bean in FacesConverter. You can use @EJB in similar fashion.

Sices answered 14/10, 2011 at 19:8 Comment(0)
L
0

You could access it indirectly through FacesContext, which is a parameter in both Converter methods.

The converter could be also annotated CDI Named with Application scope. When accessing the facade, two instances of the same class are used. One is the converter instance itself, dumb, without knowing EJB annotation. Another instance retains in application scope and could be accessed via the FacesContext. That instance is a Named object, thus it knows the EJB annotation. As everything is done in a single class, access could be kept protected.

See the following example:

@FacesConverter(forClass=Product.class)
@Named
@ApplicationScoped
public class ProductConverter implements Converter{
    @EJB protected ProductFacade facade;

    protected ProductFacade getFacadeFromConverter(FacesContext ctx){
        if(facade==null){
            facade = ((ProductConverter) ctx.getApplication()
                .evaluateExpressionGet(ctx,"#{productConverter}",ProductConverter.class))
                .facade;
        }
        return facade;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return getFacadeFromConverter(context).find(Long.parseLong(value));
    }

...
Limousine answered 27/5, 2015 at 22:36 Comment(0)
R
0

@Inject will only works in CDI managed instances

This only works at least Java EE 7 and CDI 1.1 server:

@FacesConverter
public class MyConverter implements Converter {

  protected MyService myService;    

  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
      myService = CDI.current().select(MyService .class).get();
      myService.doSomething();
  }

}
Rectangular answered 17/8, 2017 at 11:49 Comment(0)
P
-4

By Luis Chacon, Sv

Works fine, tested

definition EJB :

@Stateless
@LocalBean
public class RubroEJB {

    @PersistenceContext(unitName = "xxxxx")
    private EntityManager em;

    public List<CfgRubroPres> getAllCfgRubroPres(){
        List<CfgRubroPres> rubros = null;
        Query q = em.createNamedQuery("xxxxxxx");
        rubros = q.getResultList();
        return rubros;
    }
}

define bean with the Aplication bean scope, for get the EJB Object

@ManagedBean(name="cuentaPresService", eager = true)
@ApplicationScoped
public class CuentaPresService {

    @EJB
    private RubroEJB cfgCuentaEJB;

    public RubroEJB getCfgCuentaEJB() {
        return cfgCuentaEJB;
    }

    public void setCfgCuentaEJB(RubroEJB cfgCuentaEJB) {
        this.cfgCuentaEJB = cfgCuentaEJB;
    }
}

final Access to Ejb Object from Converter:

@FacesConverter("cuentaPresConverter")
public class CuentaPresConverter implements Converter {

    @EJB
    RubroEJB rubroEJB;

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                CuentaPresService service = (CuentaPresService) fc.getExternalContext().getApplicationMap().get("cuentaPresService");


                List<CfgCuentaPres> listCuentas=service.getCfgCuentaEJB().getAllCfgCuentaPres();


                ................
Palaeography answered 26/10, 2016 at 22:29 Comment(2)
how is this an answer to the question?Noiseless
Guess it might be an answer to the question, but without explanations its hard to tell...Filide

© 2022 - 2024 — McMap. All rights reserved.