1 Reply Latest reply on Jan 3, 2012 3:40 PM by alesj

    Creating CDI beans on demand

    srouil.samuel.rouiller.gmail.com

      Hi,


      I am quite new to CDI/Weld and doing some experiments with using CDI to perform DI in a Vaadin application.


      I defined my Vaadin application class as @SessionScoped and I am now able to use @Inject to create my views (in a MVP design) and inject my EJB services into my presenters.


      My problem with this is that all my views are injected whenever an application is started. I am afraid that this will once cause some memory usage problem.


      What I want to do is to instantiate my view explicitly when it is displayed and dispose it when it is no more used, while still having my dependencies injected and @PostConstruct methods called. This was possible with Spring @Configurable and AOP.


      Is there a way to achieve this with CDI/Weld? I am thinking about writing a producer method in each of my views, injecting the conversation scope and ending the conversation scope is a dispose() method. However, I am not sure if using the conversation scope in this context (Vaadin app) is appropriate.


      Another option would be to create my view with new XYViewComponent() and use Weld to access the BeanManager and call BeanManager.createAnnotatedType() and createInjectionTarget() to have the injection of dependencies taking place. But I am not sure that this is the right way to do it either.


      Thanks in advance for any suggestion,
      best regards,
      Samuel


       

        • 1. Re: Creating CDI beans on demand
          alesj

          Weld is already mostly on_demand, What you get at the beginning is proxies, real instances are created when needed -- as that's how you can use the right scopes, while also properly validate things at boot/deployment time.


          So, perhaps you don't even need any custom mechanism.
          I would first try if ootb works for me.