0% found this document useful (0 votes)
6 views13 pages

Java Beans

JavaBeans are reusable software components in Java that follow specific conventions, including being serializable and having a no-argument constructor. They promote encapsulation, modularity, and are commonly used in various frameworks and IDEs. Enterprise JavaBeans (EJB) is a server-side architecture for building scalable applications, offering features like transaction management and remote access.

Uploaded by

sanyamdiwan0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Java Beans

JavaBeans are reusable software components in Java that follow specific conventions, including being serializable and having a no-argument constructor. They promote encapsulation, modularity, and are commonly used in various frameworks and IDEs. Enterprise JavaBeans (EJB) is a server-side architecture for building scalable applications, offering features like transaction management and remote access.

Uploaded by

sanyamdiwan0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

INTRODUCTION TO

JAVA BEANS
REUSABLE SOFTWARE COMPONENTS IN JAVA

SHRUTI MITTAL
JCBUST, YMCA
2

JAVA BEANS
JavaBeans are reusable software components written in Java that follow a
specific set of conventions
Key Characteristics of JavaBeans:
• Serializable – Implements java.io.Serializable so the bean’s state can be saved
and restored.
• No-argument Constructor – Has a public default (no-argument) constructor.
• Getter and Setter Methods – Uses standard naming conventions for methods
to access properties:
• getPropertyName() to retrieve a value
• setPropertyName() to set a value
3

public class Student implements java.io.Serializable {


private String name;

public Student() { } // no-arg constructor

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}
4

WHY USE JAVABEANS?


• Promotes encapsulation and modularity
• Enables visual manipulation in IDEs
• Commonly used in JSP, GUI tools, and frameworks (like Spring)
• Supports introspection for automatic property discovery
JAVA BEAN VS JAVA CLASS 5

Feature JavaBean Regular Java Class


A special type of Java class that follows
Definition specific conventions for reusability and Any user-defined class in Java
introspection

May or may not have a no-argument


Constructor Must have a no-argument constructor
constructor

Serializable Typically implements Serializable Optional

Uses getter/setter methods (getX(),


Naming Convention No naming restrictions
setX())

Encapsulation Strongly follows encapsulation May or may not follow encapsulation

Designed to be reused as components,


Purpose General purpose
especially in visual IDEs or frameworks

GUI builders, JSP pages, frameworks


Used In Anywhere in Java applications
(e.g., Spring)

Supports introspection for


Introspection Support Not inherently introspectable
property/method/event discovery

Design Pattern Follows the JavaBean conventions Can follow any design pattern
PROPERTIES IN JAVABEANS 6

Type Description Example


Simple A single value
String name
Property (primitive or object)
Indexed An array or list of String[]
Property values phoneNumbers
Notifies listeners
Bound Used in GUI or
when the property
Property observer patterns
value changes
Can be vetoed by a
Constrained Useful in secure or
listener before the
Property controlled settings
value is changed
7

BEAN BUILDER

A Bean Builder (also known as a Feature Description


visual builder tool) is a graphical
environment that allows Drag-and-Drop Interface Add JavaBeans visually onto a canvas
developers to create, configure, and
connect JavaBeans components View and modify bean properties using
Property Inspector
without writing much code. getter/setter methods

Connect one bean’s event to another


Event Wiring
It lets you drag-and-drop beans, set bean’s method
their properties visually, and link
them using events and methods Load custom beans, assign icons, or
Customization
even write small scripts

Save configured beans as serialized


Persistence
objects for reuse
8

ADVANTAGES OF JAVA BEANS


Feature Benefit
Reusable Less code, more productivity
Encapsulated Safe and clean architecture
Configurable Easy to tweak in IDEs
Serializable Save/load object states
Introspectable Automatic discovery of features
Customizable GUI-friendly property editing
9

BDK INTROSPECTION
BDK (bean development kit) was a toolkit provided by sun microsystems to help developers create, test,
and visualize java beans. One of its core strengths is its support for introspection, which is the process of
analyzing a bean’s properties, events, and methods at runtime.

Introspection is the ability of a JavaBean to be examined BDK uses the java.beans.Introspector class
by builder tools or other code to determine: from the JavaBeans API to:
•What properties it has (like name, age) 1.Analyze class structure based on
•What events it can fire (like ActionEvent) standard naming (get/set/is)
•What methods it exposes (like getName(), setName()) 2.Extract metadata like:
It allows tools like BeanBox (part of BDK) to •Property descriptors (get/set
automatically discover and display bean information. methods)
•Method descriptors (public methods)
•Event set descriptors (event listeners)
10

BEANINFO INTERFACE

• Part of java.beans package. public class MyBeanBeanInfo extends SimpleBeanInfo {


• Used to customize what a builder public PropertyDescriptor[] getPropertyDescriptors() {
tool sees via introspection. try {
• You can hide, rename, or reorder PropertyDescriptor name = new PropertyDescriptor("name",
properties/methods/events. MyBean.class);
return new PropertyDescriptor[] { name };
} catch (IntrospectionException e) {
return null;
}
}
}
11

WHAT IS EJB?
• Enterprise JavaBeans (EJB) is a server-side component architecture for building modular, scalable,
transactional, and secure enterprise-level applications in Java.
• Developed as part of Java EE (now Jakarta EE), EJB simplifies the development of large, distributed
systems.
• Key features of EJB
Feature Description
Component-based EJB modules are reusable and independent
Security Declarative and programmatic security
Transaction Mgmt Built-in support for distributed transactions
Remote Access Supports remote method invocation (RMI/IIOP)
Lifecycle Mgmt Managed entirely by the EJB container
12

EJB ARCHITECTURE
13

TYPES OF EJBS
Session Beans Handle business logic; short-lived
Type Use Case
Stateless Login/authentication, single transactions
Stateful Shopping cart, session tracking
Singleton Caching, logging (shared app-wide)

Entity Beans Represent persistent data (now replaced by JPA)


Mapped to DB tables (one bean one row)
Managed by EJB container
Now deprecated in favor of JPA (Java Persistence API)

You might also like