0% found this document useful (0 votes)
35 views

Unit-2 Java Beans

Uploaded by

pappu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Unit-2 Java Beans

Uploaded by

pappu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

ADVANCE JAVA

PROGRAMMING
Unit-2
Prepared By: Ms. Ankita Sharma
Assistant Professor
Department of Computer Science
GTBIT, GGSIPU

Prepared by: Ms Ankita Sharma (Assistant Professor of CSE)


AGENDA
Java Beans

Prepared by: Ms Ankita Sharma (Assistant Professor of CSE)


Agenda
• Introduction
• Preparing a class to be a Java Bean
• Creating Java Beans
• Java Bean Properties
• Types of Beans
• Stateful Session Bean
• Stateless Session Bean
• Entity Bean
Introduction
➢Java Beans are reusable software components that adhere to a specific set of
conventions and guidelines defined by Sun Microsystems (now Oracle).
➢They are essentially Java classes that encapsulate data and functionality
making them easily accessible and manageable.
➢ Java Bean follow the principle of “Write Once, Run Anywhere” and can be
integrated seamlessly into various Java development frameworks.
➢ Overall, Java beans provides a standardized way to create reusable
components in Java, promoting modularity, encapsulating and
interoperability. They are widely used in variables Java frameworks,
libraries and applications, for building scalable and maintainable software
systems.
Introduction
➢JavaBeans is a reusable class code element often applied on java
based software application programming.
➢Here, Beans can be defined as encompassing multiple objects beneath
a single object.
➢The JavaBeans class components are methods, events, properties, and
persistence.
➢This class type is advantageous due to its notable reusability
characteristics, enables object communication, is platform-
independent, has shorter time consumption, requires lesser manual
efforts, etc.
Introduction
➢JavaBeans helps in reusing software components in developing an
application.
➢With the help of JavaBeans, one can reuse software components that
other developers write.
➢This eliminates the need for understanding the inner workings.
➢As Beans consist of numerous objects in a single object, one can
directly access this object from various places.
Introduction
➢Its maintenance is easy. The software component model deals with
creating and reusing the software components to build an application.
➢ With the help of the builder tool, one can develop new beans or use
existing beans to create an application. One can directly use these
beans in the software.
➢Consider the example of a worker assembling a car; the radio is one of
the components. Instead of manufacturing the radio from scratch, a
worker can directly assemble the radio in the car. This eliminates the
need for manufacturing a radio for assembly.
Java Class & Java bean
➢In Java, a class is a blueprint for creating objects, encapsulating both
data and methods to operate on that data.
➢A Java bean is a special type of Java class that follows specific
conventions such as having a no-argument constructor, providing
getter and setter methods for accessing its properties, and being
serializable.
➢Essentially, Java beans are a subset of Java classes designed for easy
integration with development tools and frameworks, like IDEs and
GUI builders.
Preparing a class to be a Java bean
• Java beans are the classes that encapsulate many objects into one
object, so that we can access this object from multiple places.
• A Java Bean is a Java class that should follow the following
conventions:
a) It should have a no argument constructor.
b) It should be serializable.
c) It should provide methods to set and get the values of the properties.
Java beans posses several characteristics that
set them apart from regular classes
These characteristics include:
• Public Default Constructor: Java Beans must have a public default
constructor to allow the framework or application to instantiate them.
• Private Fields with Public Accessors: Encapsulated fields within the Java
Bean should be declared as private, with public getter and setter methods to
access and modify the data.
• Serializable: Java Beans should implement the serializable interface,
enabling their state to be saved and restored.
• Event Support: Java Beans can support events by implementing
appropriate listener interfaces, allowing other components to be notified of
state changes.
Serializable
• Serialization is a mechanism of converting the state of an object into a
byte stream.
• Deserialization is the reverse process where the byte stream is used to
recreate the actual Java object in memory.
• The byte stream created is platform independent. So, the object
serialized on one platform can be deserialized on a different platform.
• To make a Java object serializable we implement the
java.io.Serializable interface.
• The ObjectOutputStream class contains writeObject() method for
serializing an Object.
Serializable
Advantages of Serializable
1.To save/persist state of an object.
2.To travel an object across a network.
Java beans Properties
• Users of JavaBean objects can access its properties.
• The feature or property being located could be of any data type,
having the classes defined by us – write, read, write-only, and read-
only.
• We can access JavaBean properties through these methods:
1. getPropertyName()
2. setPropertyName()
1. getPropertyName()
• Suppose the property name is FullName; we will be required to use
getFullName() as the method name, to read the full name of a person.
• An accessor is the name given to this method.
• Properties of this method include:
❑ It doesn’t take any argument
❑ It is public in nature
❑ It is prefixed with the term ‘get’
❑ It doesn’t have a void return type
2. setPropertyName()
• Suppose the property name is FullName, setFullName() is the method
name that we need to use to write the full name.
• Mutator is the name of this method.
Mentioned below are the properties of this method:
❑It takes some argument
❑It is public in nature
❑It is prefixed with the term ‘set’
❑It has a void return-type
Properties

Setter methods Getter methods


• It should be public in nature. • It should be public in nature.
• Return type should be void. • Return type should not be void.
• The setter method should be • The getter method should be
prefixed with the set. prefixed with get.
• It should take some argument i.e. • It should not take any argument.
it should not be a non argument
method.
Advantages
• Reusability: JavaBean is designed to be a reusable component that can be
easily integrated into other applications, which can save development time
and effort.
• Encapsulation: JavaBean encapsulates data and behavior, which helps to
keep the code modular and easy to maintain. This makes it easier to modify
and extend the code without affecting other parts of the system.
• Standardization: JavaBean follows a set of conventions and standards,
which makes it easier to understand and work with code written by others.
• Compatibility: JavaBean is compatible with a wide range of Java
technologies and frameworks, which makes them highly versatile and
adaptable to different types of projects.
Advantages
• Persistence: JavaBeans can be serialized and stored on a disk or sent
over the network, which makes it easy to save and load data in a
standardized format.
• Integration: JavaBean can be easily integrated into different Java-
based technologies and architectures, such as JavaServer Faces,
Spring, or Java EE, which makes them highly flexible and compatible.
• Platform independence: JavaBean can run on any platform that
supports the Java Virtual Machine. This makes it highly portable and
platform-independent.
Disadvantages
• Complexity: JavaBean can be complex to develop and maintain, especially
if they have a lot of properties and methods. This complexity can lead to
longer development times and an increased risk of bugs.
• Limited functionality: JavaBean is primarily designed to encapsulate data.
It can also include behavior through methods. It is not well-suited for more
complex tasks such as multithreading, networking, or user interface
programming.
• Lack of flexibility: JavaBean is designed to use as a standalone component.
This means that it can be limited in its ability to interact with other
components and systems.
• Tight coupling: JavaBean can introduce tight coupling between
components. This makes it difficult to modify or extend the system in the
future.
Simple example of JavaBean class
To access the JavaBean class, we should
use getter and setter methods.
Note: There are two ways to provide values to the object. One way
is by constructor and second is by setter method.
The bean is created by combining the
following components
• Properties define the characteristics (appearances or behaviour) of the
bean. It can be changed at design time or run time if it is not read only
and some mutator methods are given in its implementation.
• Events to communicate with other beans.
• Methods can be called from other beans or from environment or
application in that it is added.
States of Bean
Bean is a reusable software component. So as software has its
development life cycle similarly bean component has its own states.
These states define its lifecycle, and are as following:
• Construction State: In this state, the bean is under coding phase. The
developer is busy writing code for it.
• Build State: Bean is in build state, when after construction it is added
with any application.
• Run State: As a container application runs, then bean is in run state.
Note
In the context of JavaBeans, the terms "visible bean," "invisible
bean," and "container bean" are not standard terminologies.
Visible Bean:
• This term might refer to a JavaBean that is visible in some graphical
user interface (GUI) context.
• In Java GUI frameworks like Swing or JavaFX, components such as
buttons, text fields, or labels are often referred to as beans.
• A visible bean would then be one that represents a visible component
on the UI, such as a button or a text field.
Note
Invisible Bean:
• Conversely, an invisible bean could refer to a JavaBean that is not
directly visible in the GUI.
• It might represent some underlying data model, business logic, or
utility functionality that is not directly manifested as a visible
component on the UI.
• These beans are typically used for processing data or performing
operations behind the scenes.
Note
Container Bean:
• This term could refer to a JavaBean that serves as a container for other
beans.
• In the context of GUI frameworks, a container bean might represent a
component like a panel or a window that can hold other visible or
invisible beans/components.
• Container beans provide a way to organize and manage the layout of
other beans within a GUI application.
Types of Beans
JavaBeans can come in various types based on their functionality
and usage. Here are some common types:
• Standard JavaBeans: These are ordinary Java classes that adhere to
the JavaBeans conventions, such as having a public default
constructor, properties accessed through getter and setter methods, and
supporting serializability.
• Enterprise JavaBeans (EJB): EJBs are server-side components used
in Java EE (Enterprise Edition) applications for tasks like database
access, transaction management, and security. They come in three
types: Session Beans, Entity Beans, and Message-Driven Beans.
Types of Beans
• Managed Beans (JSF Managed Beans): Managed Beans are used in
JavaServer Faces (JSF) technology to manage application data and
behavior. They are typically associated with UI components and
handle user interactions.
• Spring Beans: In the Spring Framework, beans are managed objects
that are configured and managed by the Spring IoC (Inversion of
Control) container. They are often used for dependency injection and
represent various components of an application, such as services,
repositories, and controllers.
Types of Beans
• POJOs (Plain Old Java Objects): While not explicitly JavaBeans,
POJOs can be considered a type of JavaBean. They are simple Java
objects that do not require any special interfaces or inheritance,
making them easy to use and test.
• Model Beans: These beans represent data or business logic in an
application. They are commonly used in MVC (Model-View-
Controller) architecture to separate concerns and manage application
state.
Types of Beans
• Utility Beans: Utility beans provide common functionality or services
that can be reused across different parts of an application. Examples
include logging utilities, validation helpers, and data conversion tools.
• Custom Beans: Developers can create custom beans tailored to their
specific application requirements. These beans may combine features
from different types of beans or introduce unique functionality specific
to the application domain.
Java Beans Vs EJB

Java Beans EJB


• General purpose use • Enterprise purpose use
• It can be visible or invisible to • It is always invisible to the user.
the user. • EJB are distributed components
• Java Beans normally run locally. and always exist on the server.
• It can be used as ActiveX • It cant be used as ActiveX
Control. Control.
• It has no types • It has types
• It can be inherited. • No inheritance facility
Types of Enterprise Java Beans (EJB)
THANK YOU

Prepared By: Ankita Sharma (Assistant Professor of Computer


39
Science Department)

You might also like