Playing with JSR 299 - Web Beans

You can play with WebBeans! WebBeans looks nice as a web-layer-aware IoC container. I’ve been starring at Seam and Gavin to get my hands on a WebBeans implementation. Waiting. Consider my astonishment when I found the working Resin WebBeans implementation.

It’s easy to get started with the JSF example. There are also example implementations for Wicket and Struts2. WebBeans in Resin works with simple JSPs too though. Suppose we want a page which uses a web bean to display an UUID. Simple, the JSP page:

<html>
<body>
  ${uuid.value}
 </body>
</html>

and the web bean:

package de;

import javax.webbeans.Component;
import javax.webbeans.RequestScoped;
import javax.webbeans.Named;
import java.util.UUID;

@Component
@RequestScoped
@Named("uuid")
public class UUIDService {
  private String value = UUID.randomUUID().toString();

  public String getValue() { return this.value; }
}

The annotations tell the container that it’s a web beans component, we want to refer to it by the “uuid” name and the scope is a request. This means we want a new bean every request.

I had some smaller problems getting Resin working, but the nice thing about Resin are the excellent error messages. Very helpful.

Thanks for listening.


9 Responses to “Playing with JSR 299 - Web Beans”  

  1. Gravatar Icon 1 Fred

    I’m astonished too…
    How come this implementation went under the radar like that?
    Great news, finally being able to play with this Web Beans!

  2. Gravatar Icon 2 stephan

    Yes, at Caucho a lot of things seem to happen, all of them under the radar (except sometimes glimpses like the TerraCotta enabled Quercus Drupal version by Bob I think). Currently playing with a Web Beans enabled Jersey. Nice stuff.

  3. Gravatar Icon 3 ferg

    Yeah, well, we’re not very good at self-promotion :)

    Stephan, if you get some time, it would be great if you can report the problems at http://bugs.caucho.com. Some of these things are easy to fix, but just hit a blind spot in our testing.

  4. Gravatar Icon 4 stephan

    Sure I do :-) Thanks for Web Beans.

  5. Gravatar Icon 5 Stefan

    Can not believe you found this. Once it is final it will simplify web development a lot - especially for larger applications.

    Thanks for the hint!
    Stefan

  6. Gravatar Icon 6 stephan

    @Stefan: Yes it will. It makes development of a web layer below a UI toolkit much easier.

  7. Gravatar Icon 7 djo.mos

    Hi there,
    Looks promising !
    By the way, I want to mention that you can achieve this using Spring 2.5, but the bottleneck is configuration (you have to add two Spring listeners in web.xml, declare Spring el-resolver in faces-config.xml, some gore stuff in applicationContext.xml), bu once that done, it’s a joy:

    @Controller(”sthng”)
    @Scope(”session”)
    public class Something {

    }

    and the in a JSF page:

    #{sthng.somField}

    BTW, does WebBeans extend to all application layers ? I mean with Spring I can manage also my DAOS and my Service layer le DI betwwen them. But I guess WebBeans are limited to the Controller/View layer …

    Cheers :D

  8. Gravatar Icon 8 stephan

    WebBeans applies to all application layers though I guess you could integrate it with Spring or Guice should that be needed. But Web Beans is just a DI container.

  1. 1 Adding Web Beans JSR 299 to Jersey for REST at Stephans Blog


Leave a Reply



RSS