2 Replies Latest reply on Feb 28, 2013 8:23 AM by elsebbo

    Default CDI injections

    elsebbo Newbie



      I am fiddling with this problem for a long time now, but haven't come to a good result.


      I am using JSF managed beans for the user interface and EJBs for the business logic. Whenever a user logs in an initiates some action the processes get signed with the users name. For this I use the @Produces annotation from the bean which is inside of the session context. This works as expected. Besides that I need some timed services, which run outside the session context but also need some default user name. The problem now is, that I cannot use the previous injection, because a ContextNotActiveException is thrown.


      Is there any workaround, like some default production source or something? I already tried the factory pattern with a username factory, but this also leads to the ContextNotActive exception.


      What I need would be some kind of logic, which switches to a default production point, whenever the session context is not available.


      Kind regards


        • 1. Re: Default CDI injections
          Radim Hanus Expert

          I would use an application scoped bean with session scoped producer and a default producer

          btw it's not good idea to mix jsf managed beans with cdi see this great article for more details

          • 2. Re: Default CDI injections
            elsebbo Newbie

            Thanks for this link! Interesting information indeed.


            Meanwhile I managed to create some workaround, which works pretty well for my purpose:


            I designed some factory for users, which is application scoped and keeps track of all logged in users. (@WebUser in my case). The business logic uses @CurrentUser for different actions. So the code of my factory looks like the following:



            public class UserFactory {


                      @Inject @WebUser Instance<UserBean> wu;


                      @Produces @CurrentUser String getCurrentUser() {

                                try {

                               return (wu.get() != null)?wu.get().getUserId():"system";

                            } catch (Exception ex) {

                                return "system";





            The main problem was the injection of the @WebUser objects. If you use @Inject @WebUser UserBean instead of this workaround, the application will keep crashing due to the cursed ContextNotActiveException.