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

    Creating CDI beans on demand

    Samuel Rouiller Newbie


      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,