July 27, 2012

Java EE 6 Annotations - JSR 250: Common Annotations

In my previous blog I was writing about the CDI annotation in EE 6, in this blog I will write about the The Common Annotations, namely:

Resource Injection:
Managed Bean:

javax.annotation.Resource and javax.annotation.Resources

The @Resource and @Resources are shortcut for doing jndi lookup, e.g. new new InitialContext().lookup(name).

javax.annotation.PostConstruct and javax.annotation.PreDestroy

The @PostConstruct and @PreDestroy are neat annotation to do initialization and clean up in a standardized way. I would recommend to use these annotation even if you are not deploying to an enterprise environment. The above annotation can be used on any bean, such EJB, MDB, Managed Bean, but if you annotate a POJO, you need to fetch that POJO with @Inject to make the PostConstruct and PreDestroy be called.


This annotation should NOT be mixed up with javax.faces.bean.ManagedBean or JMX Managed Bean. It is a lightweight component that enables resource injection, lifecycle callbacks and interceptors. I my self is not totally sure where this component fits in, but it's there.

No comments: