-
Notifications
You must be signed in to change notification settings - Fork 0
YourNote 601: JavaBean
A JavaBean is a reusable Java class that follows a set of conventions.
A JavaBean is mainly used to:
- Store data
- Transfer data between layers
- Represent entities or models
- Encapsulate properties
A JavaBean is NOT the same as EJB (Enterprise JavaBean).
JavaBean is simply a Plain Old Java Object (POJO) that follows specific rules.
A JavaBean should:
public Student() {
}private String name;
private int age;public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}implements Serializableimport java.io.Serializable;
public class Student implements Serializable {
private String id;
private String name;
private int age;
public Student() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}public class Main {
public static void main(String[] args) {
Student student = new Student();
student.setId("S001");
student.setName("Ali");
student.setAge(20);
System.out.println(student.getId());
System.out.println(student.getName());
System.out.println(student.getAge());
}
}Output:
S001
Ali
20
Example:
private String name;Instead of:
public String name;Benefits:
- Encapsulation
- Data protection
- Validation support
- Easier maintenance
Returns a value.
public String getName() {
return name;
}Updates a value.
public void setName(String name) {
this.name = name;
}private String studentName;public String getStudentName() {
return studentName;
}public void setStudentName(String studentName) {
this.studentName = studentName;
}Field:
private boolean active;Getter:
public boolean isActive() {
return active;
}Setter:
public void setActive(boolean active) {
this.active = active;
}public void setAge(int age) {
if(age >= 0) {
this.age = age;
}
}Usage:
student.setAge(20);Benefits:
- Prevent invalid data
- Centralized validation
Example:
public class Student {
private int id;
private String name;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
}Servlet:
Student student = new Student();
student.setName("Ali");
request.setAttribute("student", student);JSP:
${student.name}or
${student.age}Entity Example:
@Entity
public class Student {
@Id
private Long id;
private String name;
public Student() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}Spring Boot heavily uses JavaBean conventions.
Any normal Java object.
public class Student {
}A POJO that follows JavaBean rules.
public class Student implements Serializable {
private String name;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}Relationship:
POJO
|
+-- JavaBean
All JavaBeans are POJOs.
Not all POJOs are JavaBeans.
| Feature | JavaBean | EJB |
|---|---|---|
| Purpose | Store Data | Business Logic |
| Server Required | No | Yes |
| Application Server | No | Yes |
| Transactions | No | Yes |
| Security | No | Yes |
| Scheduling | No | Yes |
| Complexity | Simple | Complex |
| Example | Student.java | StudentServiceEJB.java |
View (JSP)
|
Controller (Servlet)
|
Model (JavaBean)
|
Database
Example:
Student.jsp
|
StudentServlet
|
StudentBean
|
MySQL
- Reusable
- Easy to understand
- Supports encapsulation
- Framework friendly
- Easy maintenance
- Standardized naming convention
- Many getter/setter methods
- Boilerplate code
- Can become verbose
Example:
private String id;
private String name;
private int age;requires many methods.
Java 16 introduced Records.
JavaBean:
public class Student {
private String id;
private String name;
public Student() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}Record:
public record Student(
String id,
String name
) {
}Much shorter.
A JavaBean is a reusable Java class that follows standard conventions:
- Private fields
- Public getters
- Public setters
- No-argument constructor
- Serializable (recommended)
JavaBeans are commonly used for:
- Data transfer
- MVC applications
- JDBC applications
- Spring Boot entities
- DTOs (Data Transfer Objects)
JavaBean is a simple data container, while EJB is an enterprise component used for business logic and enterprise services.