3 Replies Latest reply on Dec 11, 2008 3:48 PM by rdelaplante

    WebBeans is not just for JSF and EJB right?

    rdelaplante

      Hi,


      Recently there was a quite heated discussion on Spring/EJB on JavaLobby again, which lead into WebBeans filling the gap for DI features. One of the commenters started a new blog/article on DZone to get fresh opinions. He mentions:



      Correct me if I'm wrong, but WebBeans fills the gap between component model of business layer and component model of web layer. You can easily use a EJB bean in JSF and vice versa, but what i'm talking about is using of DI as model for coupling of business logic without any dependency to web layer e.g. servlet context.

      I've read a lot of the WebBeans online book and know it's not just for mixing EJB and JSF, but since WebBeans has changed so much recently (no more @Component, no more @In) I don't really get it anymore.  Can someone knowledgeable about the usage and purpose of WebBeans please explain what it is to him?  If he doesn't learn, he and others like him reading that thread will spread the word about why people shouldn't bother looking at WebBeans and stick with Spring.



      Thanks,
      Ryan

        • 1. Re: WebBeans is not just for JSF and EJB right?
          nickarls

          Gavin wrote a shiny new article called Introduction to Web Beans which can be found under the Web Beans link to the left, that will get you up to speed. OK, the specs have changed a little since then but mostly new features so it's still 95% current.


          I think the poster just speaks about a need for a general DI mechanism in Java SE so it's a different matter. There has been talk about the Web Beans binding types etc. would be useful in SE, too but it was targetted for EE (for now?).


          When someone registers webbeanssuckusespring.org, we'll send out the full Department Of Truth ;-)

          • 2. Re: WebBeans is not just for JSF and EJB right?
            rdelaplante

            Thanks. The Introduction to WebBeans is what I read (or skimmed).  It doesn't show examples of unit testing WebBeans outside of the app server.   Also, if I have two plain POJOs it is no longer clear if I can just inject one into the other without having to create a new annotation like @CreditCardProcessor, or if the dependency is automatically injeced without saying anything because anything and everything is a webbean.  I want to see the most basic example without contextual scopes.  



            public class NewsService() {
                 private NewsDao dao;
            
                 public NewsService(NewsDao dao) {
                     this.dao = dao;
                 }
            
                 public List<NewsItem> getNews() {
                     return dao.getNews();
                 }
            }
            
            public class NewsDao() {
               ...
            }




            In Spring I would:



            <bean id="newsDao" class="com.example.product.NewsDao"/>
            <bean id="newsService" class="com.example.product.NewsService">
                <constructor-arg index="0" ref="newsDao"/>
            </bean>




            I can inject newsService into another class using this XML config or annotations, or I can get a reference to a BeanFactory and ask it to give me a newsService.  


            The last time I looked at Gavin's book/article it was not clear how to do this.   There is no more @In annotation.   There is no example of getting access to the WebBeans manager and asking it to give me an instance of newsService.  


            I'll have to read it over again... but if someone can show me on here how to do that simple example I would be happy.



            Thanks,
            Ryan

            • 3. Re: WebBeans is not just for JSF and EJB right?
              rdelaplante

              I found some of what I'm looking for.  It looks like @Current is the new @In, and that is all you need.  No need to tell WebBeans that a POJO is a WebBean by annotating it or writing XML.  This might work:




              public class NewsService() {
                   private NewsDao dao;
              
                   public NewsService(@Current NewsDao dao) {
                       this.dao = dao;
                   }
              
                   public List<NewsItem> getNews() {
                       return dao.getNews();
                   }
              }
              
              public class NewsDao() {
                 ...
              }



              I'll have to do more reading to figure out how to make sure both Service an DAO are singletons.


              I also found mention of the WebBeans Manager