EJB 3.0 Interview Questions
Which of the beans available in EJB 3 are only called by clients ?
Session and Singleton (EJB 3.1) beans are the ones who encapsulate the business logic which is why it is the only bean which is called by clients. Other beans like MDB and Entity beans are called by session and singleton beans to complete the business requirement.
Why is it so to make the session beans into two parts one with interface and the other as impl to the interface ? Or why session beans should be used through business interface ? ( EJB 3.0 Specific ).
This approach promotes loose coupling since implementation classes can easily be swapped out without a lot of code changes.But in recent EJB 3.1 specification you don't need to implement a business interface for faster development.
What is business interface ?
An interface through which a client invokes the bean is called a business interface. There are three types of business interfaces available in EJB 3 : Local, Remote and WebService.
Now in latest version of EJB the dependency injection is available , Do we still need JNDI lookups for getting the business interface ?
Yes, Our so called dependency Injection is actually a wrapper over JNDI lookups. The Injected object is actually JNDI looked up by the container and injected to the desired place. What happens inside, is that the same old JNDI lookup (which is done by the container) for our convenience.
There are places where you will have to use JNDI lookup , where container is not available. Means that, if the container is not there then how would you get the remote object ? The only answer is JNDI.
What are the criteria for a method to be a life cycle method ?
A life cycle method can be of any name and any specifier , a return type void and no argument ex : private void cleanup();
Appropriate annotation should be used. ex: @PostConstruct, @PreDestory in case of stateless beans and additional @PrePassivate and @PostActivate for stateful session beans.
It is not necessary to have a life cycle method declared in the business interfaces.Only implementation in the business implemented classes (ie Bean classes) is required.
The method should not throw any checked exception.