Plain old Java object

Last updated

In software engineering, a plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction. The term was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000: [1]

Contents

"We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely." [1]

The term "POJO" initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks. It has since gained adoption as a language-agnostic term, because of the need for a common and easily understood term that contrasts with complicated object frameworks.[ citation needed ]

The term continues an acronym pattern to coin retronyms for constructs that do not use fancy new features:

Definition

Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification; i.e. a POJO should not have to

  1. Extend prespecified classes, as in
    publicclassFooextendsjavax.servlet.http.HttpServlet{...
  2. Implement prespecified interfaces, as in
    publicclassBarimplementsjavax.ejb.EntityBean{...
  3. Contain prespecified annotations, as in
    @javax.persistence.EntitypublicclassBaz{...

However, due to technical difficulties and other reasons, many software products or frameworks described as POJO-compliant actually still require the use of prespecified annotations for features such as persistence to work properly. The idea is that if the object (actually class) were a POJO before any annotations were added, and would return to POJO status if the annotations are removed then it can still be considered a POJO. Then the basic object remains a POJO in that it has no special characteristics (such as an implemented interface) that makes it a "Specialized Java Object" (SJO or (sic) SoJO).

Contextual variations

JavaBeans

A JavaBean is a 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. Because of this convention, simple declarative references can be made to the properties of arbitrary JavaBeans. Code using such a declarative reference does not have to know anything about the type of the bean, and the bean can be used with many frameworks without these frameworks having to know the exact type of the bean. The JavaBeans specification, if fully implemented, slightly breaks the POJO model as the class must implement the Serializable interface to be a true JavaBean. Many POJO classes still called JavaBeans do not meet this requirement. Since Serializable is a marker (method-less) interface, this is not much of a burden.

The following shows an example of a JavaServer Faces (JSF) component having a bidirectional binding to a POJO's property:

<h:inputTextvalue="#{MyBean.someProperty}"/>

The definition of the POJO can be as follows:

publicclassMyBean{privateStringsomeProperty;publicStringgetSomeProperty(){returnsomeProperty;}publicvoidsetSomeProperty(StringsomeProperty){this.someProperty=someProperty;}}

Because of the JavaBean naming conventions the single "someProperty" reference can be automatically translated to the "getSomeProperty()" (or "isSomeProperty()" if the property is of Boolean type) method for getting a value, and to the "setSomeProperty(String)" method for setting a value.

Transparently adding services

As designs using POJOs have become more commonly used, systems have arisen that give POJOs the full functionality used in frameworks and more choice about which areas of functionality are actually needed. In this model, the programmer creates nothing more than a POJO. This POJO purely focuses on business logic and has no dependencies on (enterprise) frameworks. Aspect-oriented programming (AOP) frameworks then transparently add cross-cutting concerns like persistence, transactions, security, and so on. [6]

Spring was an early implementation of this idea and one of the driving forces behind popularizing this model.

An example of an EJB bean being a POJO:

The following shows a fully functional EJB bean, demonstrating how EJB3 leverages the POJO model:

publicclassHelloWorldService{publicStringsayHello(){return"Hello, world!";}}

As given, the bean does not need to extend any EJB class or implement any EJB interface and also does not need to contain any EJB annotations. Instead, the programmer declares in an external XML file which EJB services should be added to the bean:

<enterprise-beans><session><ejb-name>helloWorld</ejb-name><ejb-class>com.example.HelloWorldService</ejb-class><session-type>stateless</session-type></session></enterprise-beans>

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. [7]

Thus, as an alternative to XML, many frameworks (e.g. Spring, EJB and JPA) allow annotations to be used instead of or in addition to XML. The following shows the same EJB bean as shown above but with an annotation added. In this case the XML file is no longer needed:

@StatelesspublicclassHelloWorldService{publicStringsayHello(){return"Hello, world!";}}

With the annotation as given above the bean isn't a truly pure POJO anymore, but since annotations are merely passive metadata this has far fewer harmful drawbacks compared to the invasiveness of having to extend classes and/or implement interfaces. [6] Accordingly, the programming model is still very much like the pure POJO model.

Plain old Java Interface

A Plain old Java Interface (POJI) is a basic form of Java interface and acceptable at points where more complex Java interfaces are not permitted. [8] :57,572,576,579,1340

See also

Related Research Articles

Jakarta Enterprise Beans is one of several Java APIs for modular construction of enterprise software. EJB is a server-side software component that encapsulates business logic of an application. An EJB web container provides a runtime environment for web related software components, including computer security, Java servlet lifecycle management, transaction processing, and other web services. The EJB specification is a subset of the Java EE specification.

The Jakarta Transactions, one of the Jakarta EE APIs, enables distributed transactions to be done across multiple X/Open XA resources in a Java environment. JTA was a specification developed under the Java Community Process as JSR 907. JTA provides for:

<span class="mw-page-title-main">Jakarta EE</span> Set of specifications extending Java SE

Jakarta EE, formerly Java Platform, Enterprise Edition and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web services. Jakarta EE applications are run on reference runtimes, which can be microservices or application servers, which handle transactions, security, scalability, concurrency and management of the components they are deploying.

Java Platform, Standard Edition is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE).

In computing, the Java API for XML Processing, or JAXP, one of the Java XML Application programming interfaces, provides the capability of validating and parsing XML documents. It has three basic parsing interfaces:

Java Data Objects (JDO) is a specification of Java object persistence. One of its features is a transparency of the persistence services to the domain model. JDO persistent objects are ordinary Java programming language classes (POJOs); there is no requirement for them to implement certain interfaces or extend from special classes. JDO 1.0 was developed under the Java Community Process as JSR 12. JDO 2.0 was developed under JSR 243 and was released on May 10, 2006. JDO 2.1 was completed in Feb 2008, developed by the Apache JDO project. JDO 2.2 was released in October 2008. JDO 3.0 was released in April 2010.

<span class="mw-page-title-main">Swing (Java)</span> Java-based GUI toolkit

Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes (JFC) – an API for providing a graphical user interface (GUI) for Java programs.

Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices and service-oriented networks. Those resources are represented by objects called MBeans. In the API, classes can be dynamically loaded and instantiated. Managing and monitoring applications can be designed and developed using the Java Dynamic Management Kit.

Hibernate ORM is an object–relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate handles object–relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.

<span class="mw-page-title-main">Dependency injection</span> Software programming technique

In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function that wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' is provided with its dependencies by external code, which it is not aware of. Dependency injection makes implicit dependencies explicit and helps solve the following problems:

Apache Beehive is a discontinued Java Application Framework that was designed to simplify the development of Java EE-based applications. It makes use of various open-source projects at Apache, such as XMLBeans. Apache Beehive uses Java 5, including JSR-175, a facility for annotating fields, methods, and classes so that they can be treated in special ways by runtime tools. It builds on the framework developed for BEA Systems WebLogic Workshop for its 8.1 series. BEA later decided to donate the code to Apache.

Apache Wicket, commonly referred to as Wicket, is a component-based web application framework for the Java programming language conceptually similar to JavaServer Faces and Tapestry. It was originally written by Jonathan Locke in April 2004. Version 1.0 was released in June 2005. It graduated into an Apache top-level project in June 2007.

iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs. The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files. The result is a significant reduction in the amount of code that a developer needs to access a relational database using lower level APIs like JDBC and ODBC.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. The framework does not impose any specific programming model.. The framework has become popular in the Java community as an addition to the Enterprise JavaBeans (EJB) model. The Spring Framework is free and open source software.

Jakarta Persistence is a Jakarta EE application programming interface specification that describes the management of relational data in enterprise Java applications.

Java view technologies and frameworks are web-based software libraries that provide the user interface, or "view-layer", of Java web applications. Such application frameworks are used for defining web pages and handling the HTTP requests (clicks) generated by those web pages. As a sub-category of web frameworks, view-layer frameworks often overlap to varying degrees with web frameworks that provide other functionality for Java web applications.

Apache Empire-db is a Java library that provides a high level object-oriented API for accessing relational database management systems (RDBMS) through JDBC. Apache Empire-db is open source and provided under the Apache License 2.0 from the Apache Software Foundation.

MyBatis is a Java persistence framework that couples objects with stored procedures or SQL statements using an XML descriptor or annotations.

Composite entity is a Java EE Software design pattern and it is used to model, represent, and manage a set of interrelated persistent objects rather than representing them as individual fine-grained entity beans, and also a composite entity bean represents a graph of objects.

References

  1. 1 2 "MF Bliki: POJO". MartinFowler.com.
  2. Almaer, Dion (2006-07-17). "Return of the POJO: Plain 'Ole JavaScript". Ajaxian. Archived from the original on 2014-09-13. Retrieved 2014-08-19.
  3. "POCO Support". microsoft.com. Retrieved 2012-05-27.
  4. Kneschke, Jan (2007-02-19). "typesafe objects in PHP". kneschke.de. Archived from the original on 2012-03-26. Retrieved 2012-05-27.
  5. Cheong, Jym (2011-06-26). "Controller with bare-bone Plain Old PHP Object aka POPO". jym.sg. Archived from the original on 2012-03-26. Retrieved 2012-05-27.
  6. 1 2 Martin, Robert C; (2008); Clean Code, Chapter 11, Pure Java AOP Frameworks
  7. Panda, Debu; Rahman, Reza; Lane, Derek; (2007). EJB 3 in action, Manning Publications Co., Shelter Island (NY), ISBN   978-1-93-398834-4 (www.manning.com/books/ejb-3-in-action). Chapter 11, Deployment descriptors vs. annotations
  8. Wahli, Ueli; Vieira, Miguel; Gomes, Ferreira Lopes; Hainey, Brian; Moharram, Ahmed; Napoli, JuanPablo; Rohr, Marco; Cui, Henry; Gan, Patrick; Gonzalez, Celso; Ugurlu, Pinar; Ziosi, Lara (29 June 2009). Rational Application Developer V7.5 Programming Guide. IBM Redbooks. ISBN   978-0738432892.