You are on page 1of 6

Advance JAVA BEANS notes

05 May 2023 12:16

what is java beans

JavaBeans is a software component architecture for building reusable and modular software
applications in Java. It defines a set of rules and conventions for creating reusable software components
that can be easily integrated into larger Java applications.

A JavaBean is essentially a Java class that follows a specific set of conventions for properties, methods,
and events. These conventions include having a default constructor with no arguments, exposing
properties through getter and setter methods, implementing the Serializable interface, and providing
methods for adding and removing event listeners.

JavaBeans can be used in a variety of applications, including graphical user interfaces, web applications,
and enterprise systems. They provide a way to encapsulate and reuse code, making it easier to develop
and maintain complex software systems.

difference between java beans and enterprise java beans

JavaBeans and Enterprise JavaBeans (EJBs) are both Java technologies used for building software
components, but they have some fundamental differences in their design, scope, and usage.

JavaBeans is a lightweight component architecture designed for building reusable and modular software
components in Java. JavaBeans are typically used to encapsulate data and functionality that can be
easily integrated into larger Java applications. They are simple Java classes that follow a set of
conventions for properties, methods, and events, and they can be used in a variety of contexts,
including graphical user interfaces, web applications, and standalone applications.

Enterprise JavaBeans (EJBs), on the other hand, are a more powerful and complex component
architecture designed for building enterprise-level applications that require distributed transaction
processing, security, and scalability. EJBs are server-side components that run in a Java application
server, and they provide a range of services, including transaction management, persistence, security,
and messaging. EJBs are typically used for building large-scale enterprise applications, such as banking
systems, inventory management systems, and online marketplaces.

In summary, JavaBeans are lightweight, reusable components that can be used in a variety of contexts,
while EJBs are more complex, enterprise-level components that provide a range of services and are
typically used for building large-scale enterprise applications.

The Features of Enterprise Javabeans


Enterprise JavaBeans (EJBs) is a component architecture for building scalable, transactional, and
distributed enterprise applications in Java. Here are some of the key features of EJBs:

1. Scalability: EJBs are designed to support scalable enterprise applications. They can be deployed on
multiple servers and can be load-balanced across them to provide high availability and performance.

2. Transaction Management: EJBs provide a built-in transaction management system that ensures data
consistency and reliability. Transactions can be managed using declarative or programmatic approaches.

3. Persistence: EJBs provide a powerful persistence mechanism through the Java Persistence API (JPA),
which allows data to be stored in a database and managed by the EJB container.

4. Security: EJBs provide a robust security model that includes authentication, authorization, and
encryption. This ensures that only authorized users can access the EJBs and the data they manage.

5. Messaging: EJBs can be used to send and receive messages through the Java Messaging Service (JMS),
which provides a reliable and scalable way to exchange information between different components of
an enterprise application.

6. Asynchronous Processing: EJBs can perform long-running, asynchronous tasks without blocking the
client or other parts of the application. This is useful for handling time-consuming operations such as
data processing, file transfers, and email notifications.

7. Remote Access: EJBs can be accessed remotely using various protocols, such as Remote Method
Invocation (RMI) and Web Services. This allows EJBs to be used in distributed environments and
integrated with other applications and systems.

In summary, EJBs provide a powerful set of features for building scalable, transactional, and distributed
enterprise applications in Java.

Various Components of EJB Architecture

The Enterprise JavaBeans (EJB) architecture consists of several components that work together to
provide a powerful and flexible framework for building enterprise-level applications in Java. The main
components of the EJB architecture are:

1. EJB Container: The EJB container is a runtime environment that manages the lifecycle of EJBs,
provides transaction management, security, concurrency control, and other services required by EJBs.
The container also handles the remote communication between the EJBs and their clients.

2. EJBs: The EJBs are the business logic components that provide the functionality of the application.
There are three types of EJBs: session beans, entity beans, and message-driven beans. Each type of EJB
serves a specific purpose in the application.

3. Interfaces: EJBs expose their functionality through interfaces. There are two types of interfaces:
remote and local. Remote interfaces are used for accessing EJBs from a remote client, while local
interfaces are used for accessing EJBs from within the same application or server.

4. Deployment Descriptor: The deployment descriptor is an XML file that describes the configuration and
deployment settings for the EJBs. It includes information about the EJBs, their interfaces, transaction
management, security, and other settings required for deployment.

5. Java Naming and Directory Interface (JNDI): JNDI is a Java API that provides a naming and directory
service for Java applications. EJBs are registered with the JNDI service, which allows clients to look up
and access the EJBs by name.

6. Java Transaction API (JTA): JTA is a Java API that provides transaction management services for Java
applications. EJBs use JTA to manage transactions and ensure data consistency and integrity.
7. Java Persistence API (JPA): JPA is a Java API that provides a powerful and flexible persistence
mechanism for managing data in enterprise applications. EJBs can use JPA to store and retrieve data
from a database.

In summary, the EJB architecture consists of several components that work together to provide a
powerful and flexible framework for building enterprise-level applications in Java.

Roles and Responsibilities Provided by EJB Specification

The Enterprise JavaBeans (EJB) specification defines a set of roles and responsibilities for building
enterprise-level applications in Java. Here are the main roles and responsibilities provided by the EJB
specification:

1. EJB Developer: The EJB developer is responsible for developing the EJBs, which are the business logic
components of the application. The EJB developer creates the EJBs, defines their interfaces, implements
their methods, and configures their deployment settings.

2. EJB Container Provider: The EJB container provider is responsible for providing the runtime
environment for the EJBs. This includes implementing the EJB container, which manages the lifecycle of
the EJBs, provides transaction management, security, and other services required by the EJBs.

3. EJB Application Assembler: The EJB application assembler is responsible for assembling the EJBs into
an application. This includes creating the deployment descriptor, which describes the configuration and
deployment settings for the EJBs, and packaging the EJBs and other required files into a deployable
archive.

4. EJB Deployer: The EJB deployer is responsible for deploying the EJBs into the runtime environment.
This includes installing the EJBs into the EJB container, configuring their runtime settings, and starting
them up.

5. EJB System Administrator: The EJB system administrator is responsible for managing the runtime
environment and the deployed EJBs. This includes monitoring the performance and availability of the
EJBs, managing their security settings, and troubleshooting any issues that arise.

In summary, the EJB specification defines several roles and responsibilities for building and deploying
enterprise-level applications in Java. These roles include the EJB developer, EJB container provider, EJB
application assembler, EJB deployer, and EJB system administrator. Each role plays a critical part in the
development and deployment of EJB-based applications.

Different Types of Enterprise Javabeans

There are three types of Enterprise JavaBeans (EJBs), each designed to serve a specific purpose in
enterprise application development:

1. Session Beans: Session beans are the most commonly used type of EJBs and are used to represent the
business logic of an application. They are stateful or stateless and can be used to manage conversations
with clients. There are two types of session beans: stateful and stateless. A stateful session bean
maintains state between method invocations, while a stateless session bean does not.

2. Entity Beans: Entity beans represent persistent data in an application and are used to interact with a
database or other data store. They are typically used for transactional data such as customer records,
order details, and inventory data. Entity beans can be mapped to a database table and can be
manipulated using Java Persistence API (JPA) queries.
3. Message-Driven Beans: Message-driven beans are used for asynchronous processing in an
application. They are typically used to handle messages received from a message queue or topic.
Message-driven beans can be used to process a wide variety of messages, including JMS messages, HTTP
requests, and other types of messages.

In addition to these three types of EJBs, the EJB specification also defines two other types of beans:

4. Singleton Beans: Singleton beans are stateful session beans that have a single instance per
application. They are used for managing global application state and can be accessed by other EJBs in
the application.

5. Timer Beans: Timer beans are used for scheduling tasks in an application. They can be used to
schedule recurring tasks or one-time tasks at a specific time.

Overall, the different types of EJBs provide developers with a range of options for building enterprise-
level applications in Java. Depending on the requirements of the application, developers can choose the
appropriate type of EJB to represent the business logic, manage data, handle messages, manage global
state, and schedule tasks.

Components of Enterprise Javabeans

The components of Enterprise JavaBeans (EJB) include:

1. EJB Container: The EJB container is a runtime environment that provides a range of services to the EJB
components, including transaction management, security, and lifecycle management. It is responsible
for managing the EJBs, providing a runtime environment for their execution, and enforcing the rules
defined by the EJB specification.

2. EJB Beans: The EJB beans are the core components of the EJB architecture. They represent the
business logic of an application and are responsible for performing specific tasks. The EJB specification
defines several types of beans, including session beans, entity beans, message-driven beans, singleton
beans, and timer beans.

3. EJB Home Interface: The EJB Home Interface is a Java interface that defines the methods used to
create, find, and remove EJB components. It is used by clients to interact with the EJB container and its
components.

4. EJB Remote Interface: The EJB Remote Interface is a Java interface that defines the methods that can
be invoked by a client on a remote EJB component. It is used to communicate between the client and
the EJB container.

5. EJB Local Interface: The EJB Local Interface is similar to the EJB Remote Interface, but it is used for
communication between local EJB components within the same application server.

6. EJB Deployment Descriptor: The EJB deployment descriptor is an XML file that describes the
configuration and deployment settings for the EJB components. It includes information such as the
name and class of the EJB component, its transactional attributes, and security settings.

7. EJB Container Provider: The EJB container provider is responsible for providing the EJB container and
its associated services. It provides the runtime environment for the EJB components and ensures that
they comply with the EJB specification.
Overall, the components of EJB provide developers with a framework for building enterprise-level
applications that can be distributed, transactional, and secure. By providing a standardized set of
components and interfaces, the EJB architecture simplifies the development and deployment of
complex applications.

Create and Deploy Stateless Session Beans

To create EJB application, you need to create bean component and bean client

1) Create stateless bean component

package com.javatpoint;

import javax.ejb.Remote;

public interface AdderImplRemote {

int add(int a,int b);

package com.javatpoint;

import javax.ejb.Stateless;

public class AdderImpl implements AdderImplRemote {

public int add(int a,int b){

return a+b;

2) Create stateless bean client

package com.javatpoint;

import javax.naming.Context;

import javax.naming.InitialContext;

public class Test {

public static void main(String[] args)throws Except

Context context=new InitialContext();

AdderImplRemote remote=(AdderImplRemo
System.out.println(remote.add(32,32));

Output

Output: 64

You might also like