Enterprise JavaBeans (EJB) is one of the most important components of the Java EE Platform.
EJB components represent the business logic in scalable, multi-layer, distributed applications. They
simplify application developers’ work, allowing them to focus on their business logic. Complex matters such as security, transactions and life cycle events are handled by the EJB Container and developers only need to configure what they would like to use.
The development process of EJB is as simple as implementing the source code of the beans, packaging them into JAR files, and deploying them on the server. If you use EJB 3.0 you can optionally describe the beans in the ejb-jar.xml or use annotations instead. If you use EJB 2.x you have to use the deployment descriptor.
The SAP NetWeaver Application Server is compliant with EJB 3.0 and all previous versions of the EJB specification. When you develop new EJB applications, we recommend that you use Enterprise JavaBeans 3.0 specification.
There are three types of beans – stateful, stateless and message-driven beans. Each type is suitable for different use cases.
Use stateful session beans when you want to hold the state between method invocations. This is very useful when you use transactions. When the transaction is committed, the stateful bean becomes passive and waits for the next method invocation.
Stateless session beans do not hold the state between method invocations. This has several advantages in the cases when the state between the method invocation is not needed:
● Less server resources consumption
● Faster access.
Message-driven beans are a special type of stateless session beans that are used in asynchronous communication scenarios such as receiving JMS messages.
More information:
● Creating Session Beans in the Developer Studio
● Creating Message-Driven Beans in the Developer Studio
The life cycle of the beans depends on their type. Stateless and message-driven beans have two states - active and not existing - while stateful beans have a third state, they can be passivated.
The EJB Container creates and manages the beans’ instances. If your application logic needs to be aware of the life cycle events, you have to provide callback methods so that the EJB Container calls them at the appropriate moment.
● Life cycle of stateful session beans’ instances
● Life cycle of stateless session beans’ instances
● Life cycle of message-driven beans’ instances
In EJB 3.0 using annotations you can inject dependencies to obtain a resource in a bean or a reference to another bean.
More information: Using Dependency Injection
To access an enterprise bean from non-standard Java EE components that cannot take advantage of the JNDI environment, you have to use a new format of the lookup strings, called EJB lookup scheme.
More information: Accessing Enterprise JavaBeans from Remote Clients
Persistency has evolved in Java Persistence API (JPA). The old fashioned entity beans are still supported but for new development, we recommend that you use the JPA specification.
In EJB 3.0 the entity beans are replaced by Plain Old Java Objects (POJOs), which are managed through the Java Persistence API (JPA).
More information: Using Java Persistence API (JPA)