2. Why does DI not compatible outside the container ??
3. What is the use of @Resource annotation at the class level and how is it different from the field level ??
posted 8 years ago
It isn't really practical to answer these questions in a forum post, they are simply too extensive. I would suggest picking up a good reference on EJB 3/Java EE 5.
In short, JNDI is used as the central registry for all container managed objects such as data sources and EJBs. Java EE 5 DI will allow you to declaratively inject objects managed by the container into other managed objects, typically from JNDI.
DI cannot be processed when a container is not present simply because there is nothing to process the container annotations. That being said, you can use either application client containers or embedded containers, both of which are designed to be used outside the application servers, inside Java SE environments.
@Resource on the class is used to declare dependencies so that look-ups can be used more easily via the EJBContext. @Resouce annotations on fields/methods result in resources actually being injected, although it still has the side-effect of declaring a dependency in the EJB context.
Hope this helps,
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Hang a left on main. Then read this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user