the blog for developers

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.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

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!

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.

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.

stephan

Sure I do :-) Thanks for Web Beans.

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

stephan

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

[...] No signal, no noise. « Playing with JSR 299 – Web Beans [...]

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

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.

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType