普通旧Java对象(PO​​JO)的含义是什么? [英] What is meaning of Plain Old Java Object (POJO)?

查看:153
本文介绍了普通旧Java对象(PO​​JO)的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

普通旧Java对象(PO​​JO)是什么意思?我找不到足够的解释.

What does the term Plain Old Java Object (POJO) mean? I couldn't find anything explanatory enough.

POJO的Wikipedia页面表示POJO是普通的Java对象,而不是特殊的对象.现在,在Java中什么是特殊的,什么是不特殊的?

POJO's Wikipedia page says that POJO is an ordinary Java Object and not a special object. Now, what makes or what doesn't make and object special in Java?

上一页还说,POJO不必扩展预先指定的类,实现预先指定的接口或包含预先指定的注释.这是否还意味着不允许POJO实现诸如SerializableComparable之类的接口或诸如Applets之类的类或任何其他用户编写的类/接口?

The above page also says that a POJO should not have to extend prespecified classes, implement prespecified Interfaces or contain prespecified Annotations. Does that also mean that POJOs are not allowed to implement interfaces like Serializable, Comparable or classes like Applets or any other user-written Class/Interfaces?

此外,上述政策(不扩展,不实施)是否表示不允许我们使用任何外部库?

Also, does the above policy (no extending, no implementing) means that we are not allowed to use any external libraries?

到底在哪里使用POJO?

Where exactly are POJOs used?

编辑:更具体地说,我是否允许扩展/实现属于Java或任何外部库的类/接口?

To be more specific, am I allowed to extend/implement classes/interfaces that are part of the Java or any external libraries?

推荐答案

普通的旧Java对象该名称用于强调给定对象是普通Java对象,而不是特殊对象,例如EJB 2框架定义的那些.

Plain Old Java Object The name is used to emphasize that a given object is an ordinary Java Object, not a special object such as those defined by the EJB 2 framework.

A类{}
B类扩展/实现C {}

class A {}
class B extends/implements C {}

注意:当C是一种分布式框架类或ifc时,B是非POJO. 例如javax.servlet.http.HttpServlet,javax.ejb.EntityBean或J2EE extn 并且不可序列化/可比.由于可序列化/可比较对POJO有效.

Note: B is non POJO when C is kind of distributed framework class or ifc. e.g. javax.servlet.http.HttpServlet, javax.ejb.EntityBean or J2EE extn and not serializable/comparable. Since serializable/comparable are valid for POJO.

这里A是独立的简单对象. B是B的特殊对象,因为B正在扩展/实现C.因此B对象从C中获得了更多含义,并且B严格遵守C的规则.并且B与分布式框架紧密耦合.因此,从其定义来看,B对象不是POJO.

Here A is simple object which is independent. B is a Special obj since B is extending/implementing C. So B object gets some more meaning from C and B is restrictive to follow the rules from C. and B is tightly coupled with distributed framework. Hence B object is not POJO from its definition.

使用类的代码对象引用不必了解其类型,它可以与许多框架一起使用.

Code using class A object reference does not have to know anything about the type of it, and It can be used with many frameworks.

因此,POJO不必1)扩展预先指定的类和2)实现预先指定的接口.

So a POJO should not have to 1) extend prespecified classes and 2) Implement prespecified interfaces.

JavaBean是POJO的示例,它是可序列化的,具有无参数构造函数,并允许使用遵循简单命名约定的getter和setter方法访问属性.

JavaBean is a example of POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention.

POJO纯粹专注于业务逻辑,不依赖于(企业)框架. 这意味着它具有用于业务逻辑的代码,但是如何创建此实例,该对象属于哪个服务(EJB ..)以及它具有哪些特殊特征(有状态/无状态),将由框架使用外部xml来确定.文件.

POJO purely focuses on business logic and has no dependencies on (enterprise) frameworks. It means it has the code for business logic but how this instance is created, Which service(EJB..) this object belongs to and what are its special characteristics( Stateful/Stateless) it has will be decided by the frameworks by using external xml file.

示例1:JAXB是将Java对象表示为XML的服务;这些Java对象很简单,并带有默认的构造函数getter和setters.

Example 1: JAXB is the service to represent java object as XML; These java objects are simple and come up with default constructor getters and setters.

示例2:Hibernate,其中将使用简单的Java类来表示Table.列将为其实例.

Example 2: Hibernate where simple java class will be used to represent a Table. columns will be its instances.

示例3:REST服务.在REST服务中,我们将具有服务层和Dao层,以通过DB执行一些操作.因此,Dao将具有特定于供应商的查询和操作.服务层将负责调用哪个DAO层来执行数据库操作. DAO的创建或更新API(方法)将以POJO为参数,并更新该POJO并将其插入/更新到DB中.这些POJO(Java类)将仅具有每列的状态(实例变量)及其获取器和设置器.

Example 3: REST services. In REST services we will have Service Layer and Dao Layer to perform some operations over DB. So Dao will have vendor specific queries and operations. Service Layer will be responsible to call Which DAO layer to perform DB operations. Create or Update API(methods) of DAO will be take POJOs as arguments, and update that POJOs and insert/update in to DB. These POJOs (Java class) will have only states(instance variables) of each column and its getters and setters.

在实践中,有些人认为注释优雅,而他们却认为XML冗长,丑陋且难以维护,而另一些人则发现注释污染了POJO模型. 因此,作为XML的替代方案,许多框架(例如Spring,EJB和JPA)都允许使用注解代替XML或在XML之外使用注解:

In practice, some people find annotations elegant, while they see XML as verbose, ugly and hard to maintain, yet others find annotations pollute the POJO model. Thus, as an alternative to XML, many frameworks (e.g. Spring, EJB and JPA) allow annotations to be used instead or in addition to XML:

优点:
将应用程序代码与基础架构框架分离是使用POJO的众多好处之一.使用POJO可以通过将应用程序与不断变化的,不断变化的基础架构框架分离开来,从而证明您的应用程序的业务逻辑.升级到新版本或切换到其他框架变得更容易,风险也更低. POJO还使测试更加容易,从而简化并加速了开发.您的业​​务逻辑将更加清晰和简单,因为它不会与基础结构代码纠缠在一起

Advantages:
Decoupling the application code from the infrastructure frameworks is one of the many benefits of using POJOs. Using POJOs future proofs your application's business logic by decoupling it from volatile, constantly evolving infrastructure frameworks. Upgrading to a new version or switching to a different framework becomes easier and less risky. POJOs also make testing easier, which simplifies and accelerates development. Your business logic will be clearer and simpler because it won't be tangled with the infrastructure code

参考文献: Wiki 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆