Java的POJO和Beans
原帖位于IT老兵博客,沉淀着一个IT老兵对于这个行业的认知。
前言
想总结一下POJO和Beans,发现这个工作有人已经做了,认真地阅读,然后转帖下来,记录笔记。
正文
POJO vs Java Beans
POJO classes
POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any class path. POJOs are used for increasing the readability and re-usability of a program. POJOs have gained most acceptance because they are easy to write and understand. They were introduced in EJB 3.0 by Sun microsystems.
POJO代表着Plain Old Java Object,简单的旧的Java对象。它是一个常规的Java对象,除了被Java语言规范之外不被任何指定的限制所约束,并且不需要任何class path。POJOs被用于增加一个程序的可读性和可重用性。
A POJO should not:
- Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { … } is not a POJO class.
- Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { … } is not a POJO class.
- Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { … } is **not **a POJO class.
一个POJO不应该:
- 继承别的预先指定的类。
- 实现预先指定的接口。
- 包含预先指定的注解。
POJOs basically defines an entity. Like in you program, if you want a Employee class then you can create a POJO as follows:
// Employee POJO class to represent entity Employeepublic class Employee
{
// default field
String name;
// public field
public String id;
// private salary
private double salary;
//arg-constructor to initialize fields
public Employee(String name, String id, double salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
// getter method for name
public String getName()
{
return name;
}
// getter method for id
public String getId()
{
return id;
}
// getter method for salary
public Double getSalary()
{
return salary;
}
}
The above example is a well defined example of POJO class. As you can see, there is no restriction on access-modifier of fields. They can be private, default, protected or public. It is also not necessary to include any constructor in it.
对于域的访问和修改没有限制。
POJO is an object which encapsulates Business Logic. Following image shows a working example of POJO class. Controllers get interact with your business logic which in turn interact with POJO to access the database. In this example a database entity is represented by POJO. This POJO has the same members as database entity.
POJO是一个封装了业务逻辑的对象。控制器和你的商业逻辑打交道,然后要通过和POJO打交道去访问数据库。在这个例子里面,一个数据库实例被一个POJO锁代表。这个POJO和数据库实体拥有同样的成员。这里其实就是ORM的概念了。
Java Beans
Beans are special type of Pojos. There are some restrictions on POJO to be a bean.
- All JavaBeans are POJOs but not all POJOs are JavaBeans.
- Serializable i.e. they should implement Serializable interface. Still some POJOs who don’t implement Serializable interface are called POJOs beacause Serializable is a marker interface and therefore not of much burden.
- Fields should be private. This is to provide the complete control on fields.
- Fields should have getters or setters or both.
- A no-arg constructor should be there in a bean.
- Fields are accessed only by constructor or getter setters.
Bean是一种特殊类型的POJO。要成为一个bean,有一些对于POJO的限制。
- 所有JavaBeans都是POJO,但不是所有的POJO是JavaBeans。
- 有些POJOs不实现Serializable接口,它们仍然被称作POJO,这里和上面的POJO的特征有点冲突啊。这里说Serializable interface是一个marker的interface,什么意思,标记型的interface?
- 域可以是私有的。
- 域需要提供getter或者setter方法,或者都要。
- 需要一个无参数的构造函数。
- 域只能被构造函数或者getter和setter方法访问。
Getters and Setters have some special names depending on field name. For example, if field name is someProperty then its** getter** preferably will be:
public void getSomeProperty(){
return someProperty;
}
and **setter **will be
public void setSomePRoperty(someProperty){
this.someProperty=someProperty;
}
Visibility of getters and setters in generally public. Getters and setters provide the complete restriction on fields. e.g. consider below property,
Integer age;
If you set visibility of age to public, then any object can use this. Suppose you want that age can’t be 0. In that case you can’t have control. Any object can set it 0. But by using setter method, you have control. You can have a condition in your setter method. Similarly, for getter method if you want that if your age is 0 then it should return null, you can achieve this by using getter method as in following example:
// Java program to illustrate JavaBeansclass Bean
{
// private field property
private Integer property;
Bean()
{
// No-arg constructor
}
// setter method for property
public void setProperty(Integer property)
{
if (property == 0)
{
// if property is 0 return
return;
}
this.property=property;
}
// getter method for property
public int getProperty()
{
if (property == 0)
{
// if property is 0 return null
return null;
}
return property;
}
}
// Class to test above bean
public class GFG
{
public static void main(String[] args)
{
Bean bean = new Bean();
bean.setProperty(0);
System.out.println("After setting to 0: " +
bean.getProperty());
bean.setProperty(5);
System.out.println("After setting to valid" +
" value: " + bean.getProperty());
}
}
Output:
After setting to 0: null After setting to valid value: 5
POJO vs Java Bean
POJO | JAVA BEAN |
---|---|
It doesn’t have special restrictions other than those forced by Java language. | It is a special POJO which have some restrictions. |
It doesn’t provide much control on members. | It provides complete control on members. |
It can implement Serializable interface. | It should implement serializable interface. |
Fields can be accessed by their names. | Fields are accessed only by getters and setters. |
Fields can have any visibility. | Fields have only private visibility. |
There can be a no-arg constructor. | It must have a no-arg constructor. |
It is used when you don’t want to give restriction on your members and give user complete access of your entity | It is used when you want to provide user your entity but only some part of your entity. |
总结
看过了这篇文章,基本搞明白了POJO和Bean的概念。
参考
https://www.geeksforgeeks.org/pojo-vs-java-beans/
以上是 Java的POJO和Beans 的全部内容, 来源链接: utcz.com/z/393050.html