JavaBean과 POJO의 차이점은 무엇입니까?
차이가 확실하지 않습니다. 저는 Hibernate를 사용하고 있으며 일부 책에서는 JavaBean과 POJO를 상호 교환 가능한 용어로 사용합니다. Hibernate 컨텍스트뿐만 아니라 일반적인 개념으로 차이가 있는지 알고 싶습니다.
JavaBean은 특정 규칙을 따릅니다. getter / setter 네이밍, 퍼블릭 기본 생성자, 직렬화 가능 등. 자세한 내용은 JavaBeans Conventions 를 참조하십시오.
POJO (plain-old-Java-object)는 엄격하게 정의되지 않았습니다. 특정 인터페이스를 구현하거나 특정 기본 클래스에서 파생하거나 특정 프레임 워크와 호환되도록 특정 주석을 사용하지 않아도되는 임의의 (종종 비교적 단순 할 수 있음) Java 객체입니다. 자바 객체.
모든 JavaBean이 POJO이지만 모든 POJO가 JavaBean 인 것은 아닙니다.
JavaBean은 특정 프로그래밍 규칙을 만족시키는 Java 객체입니다.
- JavaBean 클래스는 Serializable 또는 Externalizable을 구현해야합니다.
- JavaBean 클래스에는 인수없는 공개 생성자가 있어야합니다.
- 모든 JavaBean 특성에는 공용 setter 및 getter 메소드가 있어야합니다 (적절한 경우).
- 모든 JavaBean 인스턴스 변수는 개인용이어야합니다.
Martin Fowler에 따르면 POJO는 Business Logic을 캡슐화하는 객체이며 Bean (다른 답변에 이미 정의 된 정의 제외)은 데이터를 보관하기위한 컨테이너에 지나지 않으며 객체에서 사용할 수있는 작업은 단순히 설정하고 데이터를 가져옵니다.
이 용어는 Rebecca Parsons, Josh MacKenzie와 2000 년 9 월 회의에서 연설을 준비하는 동안 만들어졌습니다.이 연설에서 우리는 비즈니스 로직을 Entity Bean을 사용하는 대신 일반 Java 객체로 인코딩 할 때 얻을 수있는 많은 이점을 지적했습니다. 우리는 왜 사람들이 시스템에서 일반 객체를 사용하는 것에 반대하는지 궁금해했고 단순한 객체에는 멋진 이름이 없기 때문이라고 결론 내 렸습니다. 그래서 우리는 그들에게 하나를 주었다. 그리고 그것은 매우 멋지게 붙 잡혔다.
Pojo-일반 Java 오브젝트
pojo 클래스는 기술 / 프레임 워크와 완전히 느슨하게 결합 된 특수 클래스가없는 일반 클래스입니다. 클래스는 기술 / 프레임 워크에서 구현하지 않으며 클래스가 pojo 클래스라고하는 기술 / 프레임 워크 API에서 확장되지 않습니다.
pojo 클래스는 인터페이스를 구현하고 클래스를 확장 할 수 있지만 수퍼 클래스 또는 인터페이스는 기술 / 프레임 워크가되어서는 안됩니다.
예 :
1.
class ABC{
----
}
ABC 클래스는 기술 / 프레임 워크를 구현하거나 확장하지 않았으므로 이것이 pojo 클래스입니다.
2.
class ABC extends HttpServlet{
---
}
서블릿 기술 API에서 확장 된 ABC 클래스는 이것이 pojo 클래스가 아닌 이유입니다.
삼.
class ABC implements java.rmi.Remote{
----
}
ABC 클래스는 rmi api에서 구현되므로 이것이 pojo 클래스가 아닙니다.
4.
class ABC implements java.io.Serializable{
---
}
이 인터페이스는 기술 / 프레임 워크의 일부가 아닌 Java 언어의 일부이므로 포조 클래스입니다.
5.
class ABC extends Thread{
--
}
여기서 thread는 Java 언어의 클래스이므로 pojo 클래스이기도합니다.
6.
class ABC extends Test{
--
}
Test 클래스가 기술 / 프레임 워크에서 확장되거나 구현되는 경우 ABC는 Test 클래스의 속성을 상속하므로 pojo 클래스가 아닙니다. Test 클래스가 pojo 클래스가 아닌 경우 ABC 클래스도 pojo 클래스가 아닙니다.
7.
지금이 시점은 예외적 인 경우입니다
@Entity
class ABC{
--
}
@Entity
is an annotation given by hibernate api or jpa api but still we can call this class as pojo class. class with annotations given from technology/framework is called pojo class by this exceptional case.
POJO: If the class can be executed with underlying JDK,without any other external third party libraries support then its called POJO
JavaBean: If class only contains attributes with accessors(setters and getters) those are called javabeans.Java beans generally will not contain any bussiness logic rather those are used for holding some data in it.
All Javabeans are POJOs but all POJO are not Javabeans
POJOS
with certain conventions (getter/setter,public no-arg constructor ,private variables) and are in action(ex. being used for reading data by form) are JAVABEANS
.
In summary: similarities and differences are:
java beans: Pojo:
-must extends serializable -no need to extends or implement.
or externalizable.
-must have public class . - must have public class
-must have private instance variables. -can have any access specifier variables.
-must have public setter and getter method. - may or may not have setter or getter method.
-must have no-arg constructor. - can have constructor with agruments.
All JAVA Beans are POJO but not all POJOs are JAVA Beans.
You've seen the formal definitions above, for all they are worth.
But don't get too hung up on definitions. Let's just look more at the sense of things here.
JavaBeans are used in Enterprise Java applications, where users frequently access data and/or application code remotely, i.e. from a server (via web or private network) via a network. The data involved must therefore be streamed in serial format into or out of the users' computers - hence the need for Java EE objects to implement the interface Serializable. This much of a JavaBean's nature is no different to Java SE application objects whose data is read in from, or written out to, a file system. Using Java classes reliably over a network from a range of user machine/OS combinations also demands the adoption of conventions for their handling. Hence the requirement for implementing these classes as public, with private attributes, a no-argument constructor and standardised getters and setters.
Java EE applications will also use classes other than those that were implemented as JavaBeans. These could be used in processing input data or organizing output data but will not be used for objects transferred over a network. Hence the above considerations need not be applied to them bar that the be valid as Java objects. These latter classes are referred to as POJOs - Plain Old Java Objects.
All in all, you could see Java Beans as just Java objects adapted for use over a network.
There's an awful lot of hype - and no small amount of humbug - in the software world since 1995.
참고URL : https://stackoverflow.com/questions/1394265/what-is-the-difference-between-a-javabean-and-a-pojo
'Programing' 카테고리의 다른 글
현지화 및 국제화, 차이점은 무엇입니까? (0) | 2020.05.09 |
---|---|
일치하는 줄 다음에 다음 줄만 표시하는 방법? (0) | 2020.05.09 |
ADT에 'org.eclipse.wst.sse.core 0.0.0'이 필요하지만 찾을 수 없습니다 (0) | 2020.05.09 |
Linux에서 세그멘테이션 오류로 코어 덤프를 생성하는 방법은 무엇입니까? (0) | 2020.05.09 |
STL 맵에서 map :: insert를 사용하는 것이 []보다 낫습니까? (0) | 2020.05.09 |