Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:13:40+00:00 2026-05-26T00:13:40+00:00

How can I inject a dependency like @EJB , @PersistenceContext , @Inject , @AutoWired

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:13:40+00:00Added an answer on May 26, 2026 at 12:13 am

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I inject a dependency like @EJB , @PersistenceContext , @Inject , @AutoWired
When using Ninjects ConstructorArgument you can specify the exact value to inject to specific
Let me have two very basic objects like: public class View { public View(Controller
If I have a class like so: public SomeClass : ISomeClass { public SomeClass(IInjectedDependency
Is there any way we can inject new methods and properties into classes during
I wonder, if I can inject a list of (stateless) beans, that all implementing
How can I use the Prototype library and create unobtrusive javascript to inject the
I can't seem to figure out how to inject a disabled element. Any help
I have a WPF UserControl that I'd like to inject dependencies into. What's the
I have a custom IAuthorizationPolicy which has a dependency on a repository internal class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.