2 Replies Latest reply on Feb 20, 2010 6:02 PM by wujek

    Add bean before validation

    wujek

      Hi.


      I have been playing with Weld for the last few days, Weld SE to be specific. I know how to create a custom extension to register my own bean in the AfterBeanDiscovery (great info in the Weld reference doc). However, my scenario is slightly different.


      Namely: I start up Weld, and some of my beans have a dependency on a certain bean. This bean's implementation is not available in the bean archive, but I have an instance available created elsewhere, which I would like to 'put' into the bean manager, so that other beans can have it injected. The problem is that when I start Weld SE using new StartMain().go(), the archive is read, the beans are deployed, and validated. One injection point is not satisfied, so I get a deployment error befpre I even get the chance to put my bean. The AfterBeanDiscovery event is too late too - I would need to put my bean before validation. Is there a way to do this? I would like to learn a portable way is such exists, but Weld specific approach is Ok.



      For now, I created my own implementation of Bean<T>, which uses the instance I have handy and returns it from the create() call, and destroy is a no-op. I also changed the code to bootstrap Weld SE - now the StartMain.go() method takes a vararg of Bean<?> and after the deployment stage, just before validation, I iterate over the array and add the beans to the BeanManagerImpl. I call this method with my custom bean instance wrapped around this Bean<T> implementation. This works fine, the module validates now, but somehow I feel the solution is overcomplicated. Is there another way, like using @Alternatives in some clever way?
      As a side question - how can I add new bean definitions apart from using AfterBeanDiscovery.addBean()? I couldn't find a different way other than using Weld specific BeanManagerImpl and calling addBean().


      Best regards,
      Wujek


      PS Gavin, if you are reading this, I apologize for harassing you via email.