Related
I understood, I think, that a "Bean" is a Java-class with properties and getters/setters.
As much as I understand, it is the equivalent of a C struct. Is that true?
Also, is there a real syntactic difference between a JavaBean and a regular class?
Is there any special definition or an Interface?
Basically, why is there a term for this?
Also what does the Serializable interface mean?
A JavaBean is just a standard. It is a regular Java class, except it follows certain conventions:
All properties are private (use getters/setters)
A public no-argument constructor
Implements Serializable.
That's it. It's just a convention. Lots of libraries depend on it though.
With respect to Serializable, from the API documentation:
Serializability of a class is enabled by the class implementing the
java.io.Serializable interface. Classes that do not implement this
interface will not have any of their state serialized or deserialized.
All subtypes of a serializable class are themselves serializable. The
serialization interface has no methods or fields and serves only to
identify the semantics of being serializable.
In other words, serializable objects can be written to streams, and hence files, object databases, anything really.
Also, there is no syntactic difference between a JavaBean and another class -- a class is a JavaBean if it follows the standards.
There is a term for it, because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the library requires your objects be proper JavaBeans).
There's a term for it to make it sound special. The reality is nowhere near so mysterious.
Basically, a "Bean":
is a serializable object (that is, it implements java.io.Serializable, and does so correctly), that
has "properties" whose getters and setters are just methods with certain names (like, say, getFoo() is the getter for the "Foo" property), and
has a public zero-argument constructor (so it can be created at will and configured by setting its properties).
As for Serializable: That is nothing but a "marker interface" (an interface that doesn't declare any functions) that tells Java that the implementing class consents to (and implies that it is capable of) "serialization" -- a process that converts an instance into a stream of bytes. Those bytes can be stored in files, sent over a network connection, etc., and have enough information to allow a JVM (at least, one that knows about the object's type) to reconstruct the object later -- possibly in a different instance of the application, or even on a whole other machine!
Of course, in order to do that, the class has to abide by certain limitations. Chief among them is that all instance fields must be either primitive types (int, bool, etc.), instances of some class that is also serializable, or marked as transient so that Java won't try to include them. (This of course means that transient fields will not survive the trip over a stream. A class that has transient fields should be prepared to reinitialize them if necessary.)
A class that can not abide by those limitations should not implement Serializable (and, IIRC, the Java compiler won't even let it do so.)
JavaBeans are Java classes which adhere to an extremely simple coding convention.
All you have to do is to
implement the java.io.Serializable interface - to save the state of an
object
use a public empty argument constructor - to instantiate the object
provide public getter/setter methods - to get and set the values of private variables (properties).
Properties of JavaBeans
A JavaBean is a Java object that satisfies certain programming conventions:
The JavaBean class must implement either Serializable or
Externalizable
The JavaBean class must have a no-arg constructor
All JavaBean properties must have public setter and getter methods
All JavaBean instance variables should be private
Example of JavaBeans
#Entity
public class Employee implements Serializable{
#Id
private int id;
private String name;
private int salary;
public Employee() {}
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
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;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
Explanation with an example.
1. import java.io.Serializable
As for the Serialization, see the documentation.
2. private fields
Fields should be private for prevent outer classes to easily modify those fields.
Instead of directly accesing to those fields, usuagly getter/setter methods are used.
3. Constructor
A public constructor without any argument.
4. getter/setter
Getter and setter methods for accessing and modifying private fields.
/** 1. import java.io.Serializable */
public class User implements java.io.Serializable {
/** 2. private fields */
private int id;
private String name;
/** 3. Constructor */
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
/** 4. getter/setter */
// getter
public int getId() {
return id;
}
public String getName() {
return name;
}
// setter
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
Java Beans are used for a less code and more work approach...
Java Beans are used throughout Java EE as a universal contract for runtime discovery and access. For example, JavaServer Pages (JSP) uses Java Beans as data transfer objects between pages or between servlets and JSPs. Java EE's JavaBeans Activation Framework uses Java Beans for integrating support for MIME data types into Java EE. The Java EE Management API uses JavaBeans as the foundation for the instrumentation of resources to be managed in a Java EE environment.
About Serialization:
In object serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
You will find serialization useful when deploying your project across multiple servers since beans will be persisted and transferred across them.
Just a little background/update on the bean concept. Many other answers actually have the what but not so much why of them.
They were invented early on in Java as part of building GUIs. They followed patterns that were easy for tools to pull apart letting them create a properties panel so you could edit the attributes of the Bean. In general, the Bean properties represented a control on the screen (Think x,y,width,height,text,..)
You can also think of it as a strongly typed data structure.
Over time these became useful for lots of tools that used the same type of access (For example, Hibernate to persist data structures to the database)
As the tools evolved, they moved more towards annotations and away from pulling apart the setter/getter names. Now most systems don't require beans, they can take any plain old Java object with annotated properties to tell them how to manipulate them.
Now I see beans as annotated property balls--they are really only useful for the annotations they carry.
Beans themselves are not a healthy pattern. They destroy encapsulation by their nature since they expose all their properties to external manipulation and as they are used there is a tendency (by no means a requirement) to create code to manipulate the bean externally instead of creating code inside the bean (violates "don't ask an object for its values, ask an object to do something for you"). Using annotated POJOs with minimal getters and no setters is much more OO restoring encapsulation and with the possibility of immutability.
By the way, as all this stuff was happening someone extended the concept to something called Enterprise Java Beans. These are... different. and they are complicated enough that many people felt they didn't understand the entire Bean concept and stopped using the term. This is, I think, why you generally hear beans referred to as POJOs (since every Java object is a POJO this is technically OK, but when you hear someone say POJO they are most often thinking about something that follows the bean pattern)
JavaBeans is a standard, and its basic syntax requirements have been clearly explained by the other answers.
However, IMO, it is more than a simple syntax standard. The real meaning or intended usage of JavaBeans is, together with various tool supports around the standard, to facilitate code reuse and component-based software engineering, i.e. enable developers to build applications by assembling existing components (classes) and without having to write any code (or only have to write a little glue code). Unfortunately this technology is way under-estimated and under-utilized by the industry, which can be told from the answers in this thread.
If you read Oracle's tutorial on JavaBeans, you can get a better understanding in that.
For a Java class to be usable as a Java bean, its method names need to be as per the JavaBeans guidelines (also called design patterns) for properties, methods, and events. The class needs to be a public class to be accessible to any beanbox tool or container. The container must be able to instantiate it; with the class as public, the container should be able to do so even if no explicit, public, zero-args constructor is provided. (A Java public class with no explicit constructor has a default public zero-args constructor.) So, minimally, a Java public class, even with a property as the sole member (of course, accompanying public getter and setter required) or a public method as the sole member, is a Java bean. The property can either be a read-only property (it has a getter method but no setter) or write-only property (has a setter method only). A Java public class with a public event listener registration method as the sole member is also a Java bean. The JavaBeans specification doesn’t require that if such a Java class has an explicit public constructor, it should be a zero-args one. If one could provide a file (with an extension, say, .ser) containing a serialized instance, a beanbox tool may be able to use that file to instantiate a prototype bean. Otherwise, the class would need a constructor, either explicit or default, that is public as well as zero-args.
Once the bean is instantiated, the JavaBeans API ( java.beans.*) can introspect it and call methods on it. If no class implementing the interface BeanInfo or extending a BeanInfo implementation,such as the SimpleBeanInfo class, is available, the introspection involves using reflection (implicit introspection) to study the methods supported by a target bean and then applying simple design patterns(the guidelines) to deduce from those methods what properties, events, and public methods are supported. If a class implementing the interface BeanInfo (for a bean Foo, it must be named FooBeanInfo) is available, the API bypasses implicit introspection and uses public methods (getPropertyDescriptor(), getMethodDescriptors(), getEventSetDescriptors() ) of this class to get the information. If a class extending SimpleBeanInfo is available, depending on which of the SimpleBeanInfo public methods (getPropertyDescriptor(), getMethodDescriptors(), getEventSetDescriptors() ) are overridden, it will use those overridden methods(s) to get information; for a method that is not overridden, it’ll default to the corresponding implicit introspection. A bean needs to be instantiated anyway, even if no implicit introspection is carried out on it. Thus, the requirement of a public zero-args constructor. But, of course, the Serializable or Externalizable interface isn’t necessary for it to be recognized. However, the JavaBeans specification says, ‘We’d also like it to be “trivial” for the common case of a tiny Bean that simply wants to have its internal state saved and doesn’t want to think about it.’ So, all beans must implement Serializable or Externalizable interface.
Overall, the JavaBeans specification isn’t hard and fast about what constitutes a bean. "Writing JavaBeans components is surprisingly easy. You don't need a special tool and you don't have to implement any interfaces. Writing beans is simply a matter of following certain coding conventions. All you have to do is make your class look like a bean — tools that use beans will be able to recognize and use your bean." Trivially, even the following class is a Java bean,
public class Trivial implements java.io.Serializable {}
The description so far is the Java SE version (JavaBeans). The beans, as described below, are the Java EE versions. These versions have been built on the underlying ideas as explained above. In particular, one main idea they consider is what if a bean constructor does have some parameters. These parameters could be either simple types, class/interface types or both. There should be a way to let the container know values that it can substitute for the parameters when instantiating the bean. The way to do so is that the programmer can configure (specify values) by say annotations or XML configuration files or a mix of both.
Spring Beans
Spring beans run in a Spring IoC container. The programmer can configure via XML configuration files, annotations or a mix of both.
In Spring, if a bean constructor has simple-type or class/interface type parameters, values can be assigned as strings (as the <value> attribute of a constructor argument element in the former case and as an <idref> element of a constructor argument in the latter case) in a type-safe manner. Making references to other Spring beans (called collaborators; via the <ref> element in a constructor argument element) is basically dependency injection and is also typesafe. Obviously, a dependency (collaborator bean) might have a constructor with injected parameters; those injected dependency(ies) might have a constructor with parameters and so on. This scenario should ultimately terminate at injected dependency(ies) that are prototype beans that the container can instantiate by constructing.
JSF Managed Beans
JSF managed beans run in a web container. They can be configured either with the #ManagedBean annotation or with an application configuration resource file managed-bean.xml. The JSF spec supports injection via resource injection (not typesafe) only. This injection is not fit for injection on constructors. In any case, the spec requires that a JSF managed bean must have a public zero-argument constructor. Further it says, “As of version 2.3 of this specification, use of the managed bean facility as specified in this section is strongly
discouraged. A better and more cohesively integrated solution for solving the same problem is to use Contexts and Dependency Injection (CDI), as specified in JSR-365." In other words, CDI managed beans should be used, which do offer typesafe dependency injection on constructors akin to Spring beans. The CDI specification adopts the Managed Beans specification, which applies to all containers of the JEE platform, not just the web tier. Thus, the web container needs to implement the CDI specification.
Managed Beans
Here is an extract from the Managed Bean specification
“ Managed Beans are container-managed objects with minimal requirements,
otherwise known under the acronym “POJOs” (Plain Old Java Objects)…they can be seen as a Java EE platform-enhanced version of the JavaBeans component model found on the Java SE platform…It won’t be missed by the reader that Managed Beans have a precursor in the homonymous facility found in the JavaServer Faces (JSF) technology…Managed Beans as defined in this specification represent a generalization of those found in JSF; in particular, Managed Beans can be used anywhere in a Java EE application, not just in web modules. For example, in the basic component model, Managed Beans must provide a no-argument constructor, but a specification that builds on Managed Beans, such as CDI (JSR-299), can relax that requirement and allow Managed Beans to provide constructors with more complex signatures, as long as they follow some well-defined rules...A Managed Bean must not be: a final class, an abstract class, or a non-static inner class. A Managed Bean may not be serializable unlike a regular JavaBean component.”
Thus, the specification for Managed Beans, otherwise known as POJOs or POJO beans, allows extension as in CDI.
CDI Beans
The CDI specification re-defines managed beans as:
When running in Java EE, a top-level Java class is a managed bean if it meets the requirements:
• It is not an inner class.
• It is a non-abstract class, or is annotated #Decorator.
• It does not implement javax.enterprise.inject.spi.Extension.
• It is not annotated #Vetoed or in a package annotated #Vetoed.
• It has an appropriate constructor, either: the class has a constructor with no parameters, or the class declares a constructor annotated #Inject.
All Java classes that meet these conditions are managed beans and thus no special declaration is
required to define a managed bean. Or
if it is defined to be a managed bean by any
other Java EE specification and if
• It is not annotated with an EJB component-defining annotation or declared as an EJB bean class
in ejb-jar.xml.
Bean constructors can have simple-type parameters since simple-types can be injected with the #Inject annotation.
EJBs
EJBs run in an EJB container. The EJB specification says: “A session bean component is a Managed Bean." “The class must have a public constructor that takes no arguments,” it says for both session bean and message-driven bean. Furthermore, it says, “The session bean class is not required to implement the SessionBean interface or the Serializable interface.” For the same reason as JSF beans, that EJB3 dependency injection is basically resource injection, JSF beans do not support constructors with arguments, that is, via dependency injection. However, if the EJB container implements CDI, “ Optionally: The class may have an additional constructor annotated with the Inject annotation, “ it says for both session bean and message-driven bean because, “An EJB packaged into a CDI bean archive and not annotated with javax.enterprise.inject.Vetoed annotation, is considered a CDI-enabled bean.”
As per the Wikipedia:
The class must have a public default constructor (with no arguments). This allows easy instantiation within editing and activation frameworks.
The class properties must be accessible using get, set, is (can be used for boolean properties instead of get), and other methods (so-called accessor methods and mutator methods) according to a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more than one argument.
The class should be serializable. (This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.)
For more information follow this link.
Regarding the second part of your question, serialization is a persistence mechanism used to store objects as a sequence of signed bytes. Put less formally, it stores the state of an object so you can retrieve it later, by deserialization.
A Java Bean is a Java class (conceptual) that should follow the following conventions:
It should have a no-argument constructor.
It should be serializable.
It should provide methods to set and get the values of the properties, known as getter and setter methods.
It is a reusable software component. It can encapsulate many objects into one object so that same object can be accessed from multiples places and is a step towards easy maintenance of code.
They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java. According to Wikipedia.
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. According to Spring IoC.
It was repeated 6 or 7 times above that there is a no-argument constructor requirement for JavaBeans.
This is WRONG, there is no such requirement, especially in the context of Java Spring.
There is also no mention of that requirement in version (1.01) of the specification that describes the JavaBeanns APIs (https://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/). Even more - this specification mentions 'null constructor' only 2 times in the following contexts:
"Each customizer should have a null constructor."
"Each PropertyEditor should have a null constructor."
So, it does not seem like the authors of the spec don't know or are not willing to use the term "null constructor", still no mention of it for the JavaBeans proper.
A Java Bean is any Java class that satisfies the following three criteria:
It should implement the serializable interface (a Marker interface).
The constructor should be public and have no arguments (what other people call a "no-arg constructor").
It should have getter and setters.
Good to note the serialVersionUID field is important for maintaining object state.
The below code qualifies as a bean:
public class DataDog implements java.io.Serializable {
private static final long serialVersionUID = -3774654564564563L;
private int id;
private String nameOfDog;
// The constructor should NOT have arguments
public DataDog () {}
/** 4. getter/setter */
// Getter(s)
public int getId() {
return id;
}
public String getNameOfDog() {
return nameOfDog;
}
// Setter(s)
public void setId(int id) {
this.id = id;
}
public void setNameOfDog(String nameOfDog) {
this.nameOfDog = nameOfDog;
}}
If you are familiar with C/Golang, you never heard C bean or Go bean because they have struct keyword, that developers can easily define structure types without writing complicated OOP keywords.
type User struct {
Name string
Age int
}
var user User
user.Name = "name"
user.Age = 18
var bytes, err = json.Marshal(user)
It's Java's mistake that lack of struct types, and developers find this bad shortage.
Then Java Bean is invented as just another boring rule to make class pretending struct, peace your editor or compiler won't be crying or yelling about your unsafe access to class members.
To understand JavaBean you need to notice the following:
JavaBean is conceptual stuff and can not represent a class of specific things
JavaBean is a development tool can be visualized in the operation of reusable software components
JavaBean is based on the Sun JavaBeans specification and can be reusable components. Its biggest feature is the re-usability.
POJO (plain old Java object): POJOs are ordinary Java objects, with no restriction other than those forced by the Java Language.
Serialization: It is used to save state of an object and send it across a network. It converts the state of an object into a byte stream. We can recreate a Java object from the byte stream by process called deserialization.
Make your class implement java.io.Serializable interface. And use writeObject() method of ObjectOutputStream class to achive Serialization.
JavaBean class: It is a special POJO which have some restriction (or convention).
Implement serialization
Have public no-arg constructor
All properties private with public getters & setter methods.
Many frameworks - like Spring - use JavaBean objects.
If you want to understand Java-Beans, you first have to understand software-components.
Software components
A software-component is a part of an application that runs a specific operation. A software component can also be part of a service.
A component is:
Coupled (has dependencies)
Statefull (it saves the states of instance variables)
Not standarised, it is designed for a specific use case (main difference between Java-EE Beans)
Runs in client machine
Java Beans (Enterprise Beans)
Standarised components that run in a Java EE-server
Including different business logics to complete a specific service
Simplify development of complex multilayer distributed systems
Java Beans are more of a concept to manage big systems. Thats why they need standarization.
Source
In practice, Beans are just objects which are handy to use. Serializing them means to be able easily to persist them (store in a form that is easily recovered).
Typical uses of Beans in real world:
simple reusable objects POJO (Plain Old Java Objects)
visual objects
Spring uses Beans for objects to handle (for instance, User object that needs to be serialized in session)
EJB (Enterprise Java Beans), more complex objects, like JSF Beans (JSF is old quite outdated technology) or JSP Beans
So in fact, Beans are just a convention / standard to expect something from a Java object that it would behave (serialization) and give some ways to change it (setters for properties) in a certain way.
How to use them, is just your invention, but most common cases I enlisted above.
A Java Bean is a component or the basic building block in the JavaBeans architecture. The JavaBeans architecture is a component architecture that benefits from reusability and interoperability of a component-based approach.
A valid component architecture should allow programs to be assembled from
software building blocks (Beans in this case), perhaps provided by different vendors and also make it possible for an architect / developer to select a component (Bean), understand its capabilities, and incorporate it into an application.
Since classes/objects are the basic building blocks of an OOP language like Java, they are the natural contenders for being the Bean in the JavaBeans architecture.
The process of converting a plain Java class to a Java bean is actually nothing more than making it a reusable and interoperable component. This would translate into a Java class having abilities like:
controlling the properties, events, and methods of a class that are exposed to another application. (You can have a BeanInfo class that reports only those properties, events and methods that the external application needs.)
persistence (being serialisable or externizable - this would also imply having no-argument constructors, using transient for fields)
ability to register for events and also to generate events (e.g., making use of bound and constraint properties)
customizers (to customise the Bean via GUIs or by providing documentation)
In order for a Java class to be termed a Java bean it is not necessary that they need to possess all the above abilities. Instead, it implies to implement a subset of the above relevant to the context (e.g., a bean in a certain framework may not need customizers, some other bean may not need bound and constrained properties, etc.)
Almost all leading frameworks and libraries in Java adhere to the JavaBeans architecture implicitly, in order to reap the above benefits.
Spring #Bean annotation indicates that a method produces a bean to be managed by the Spring container.
More reference: https://www.concretepage.com/spring-5/spring-bean-annotation
The no-argument constructor is a
requirement (tools like Hibernate use
reflection on this constructor to
instantiate objects).
I got this hand-wavy answer but could somebody explain further? Thanks
Hibernate, and code in general that creates objects via reflection use Class<T>.newInstance() to create a new instance of your classes. This method requires a public no-arg constructor to be able to instantiate the object. For most use cases, providing a no-arg constructor is not a problem.
There are hacks based on serialization that can work around not having a no-arg constructor, since serialization uses jvm magic to create objects without invoking the constructor. But this is not available across all VMs. For example, XStream can create instances of objects that don't have a public no-arg constructor, but only by running in a so-called "enhanced" mode which is available only on certain VMs. (See the link for details.) Hibernate's designers surely chose to maintain compatibility with all VMs and so avoids such tricks, and uses the officially supported reflection method Class<T>.newInstance() requiring a no-arg constructor.
Erm, sorry everyone, but Hibernate does not require that your classes must have a parameterless constructor. The JPA 2.0 specification requires it, and this is very lame on behalf of JPA. Other frameworks like JAXB also require it, which is also very lame on behalf of those frameworks.
(Actually, JAXB supposedly allows entity factories, but it insists on instantiating these factories by itself, requiring them to have a --guess what-- parameterless constructor, which in my book is exactly as good as not allowing factories; how lame is that!)
But Hibernate does not require such a thing.
Hibernate supports an interception mechanism, (see "Interceptor" in the documentation,) which allows you to instantiate your objects with whatever constructor parameters they need.
Basically, what you do is that when you setup hibernate you pass it an object implementing the org.hibernate.Interceptor interface, and hibernate will then be invoking the instantiate() method of that interface whenever it needs a new instance of an object of yours, so your implementation of that method can new your objects in whatever way you like.
I have done it in a project and it works like a charm. In this project I do things via JPA whenever possible, and I only use Hibernate features like the interceptor when I have no other option.
Hibernate seems to be somewhat insecure about it, as during startup it issues an info message for each of my entity classes, telling me INFO: HHH000182: No default (no-argument) constructor for class and class must be instantiated by Interceptor, but then later on I do instantiate them by interceptor, and it is happy with that.
To answer the "why" part of the question for tools other than Hibernate, the answer is "for absolutely no good reason", and this is proven by the existence of the hibernate interceptor. There are many tools out there that could have been supporting some similar mechanism for client object instantiation, but they don't, so they create the objects by themselves, so they have to require parameterless constructors. I am tempted to believe that this is happening because the creators of these tools think of themselves as ninja systems programmers who create frameworks full of magic to be used by ignorant application programmers, who (so they think) would never in their wildest dreams have a need for such advanced constructs as the... Factory Pattern. (Okay, I am tempted to think so. I don't actually think so. I am joking.)
Hibernate instantiates your objects. So it needs to be able to instantiate them. If there isn't a no-arg constructor, Hibernate won't know how to instantiate it, i.e. what argument to pass.
The hibernate documentation says:
4.1.1. Implement a no-argument constructor
All persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using Constructor.newInstance(). It is recommended that you have a default constructor with at least package visibility for runtime proxy generation in Hibernate.
The hibernate is an ORM framework which supports field or property access strategy. However, it does not support constructor-based mapping - maybe what you would like ? - because of some issues like
1º What happens whether your class contains a lot of constructors
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) { ... }
public Person(String name) { ... }
public Person(Integer age) { ... }
}
As you can see, you deal with a issue of inconsistency because Hibernate cannot suppose which constructor should be called. For instance, suppose you need to retrieve a stored Person object
Person person = (Person) session.get(Person.class, <IDENTIFIER>);
Which constructor should Hibernate call to retrieve a Person object ? Can you see ?
2º And finally, by using reflection, Hibernate can instantiate a class through its no-arg constructor. So when you call
Person person = (Person) session.get(Person.class, <IDENTIFIER>);
Hibernate will instantiate your Person object as follows
Person.class.newInstance();
Which according to API documentation
The class is instantiated as if by a new expression with an empty argument list
Moral of the story
Person.class.newInstance();
is similar To
new Person();
Nothing else
Hibernate needs to create instances as result of your queries (via reflection), Hibernate relies on the no-arg constructor of entities for that, so you need to provide a no-arg constructor. What is not clear?
Actually, you can instantiate classes which have no 0-args constructor; you can get a list of a class' constructors, pick one and invoke it with bogus parameters.
While this is possible, and I guess it would work and wouldn't be problematic, you'll have to agree that is pretty weird.
Constructing objects the way Hibernate does (I believe it invokes the 0-arg constructor and then it probably modifies the instance's fields directly via Reflection. Perhaps it knows how to call setters) goes a little bit against how is an object supposed to be constructed in Java- invoke the constructor with the appropriate parameters so that the new object is the object you want. I believe that instantiating an object and then mutating it is somewhat "anti-Java" (or I would say, anti pure theoretical Java)- and definitely, if you do this via direct field manipulation, it goes encapsulation and all that fancy encapsulation stuff.
I think that the proper way to do this would be to define in the Hibernate mapping how an object should be instantiated from the info in the database row using the proper constructor... but this would be more complex- meaning both Hibernate would be even more complex, the mapping would be more complex... and all to be more "pure"; and I don't think this would have an advantage over the current approach (other than feeling good about doing things "the proper way").
Having said that, and seeing that the Hibernate approach is not very "clean", the obligation to have a 0-arg constructor is not strictly necessary, but I can understand somewhat the requirement, although I believe they did it on purely "proper way" grounds, when they strayed from the "proper way" (albeit for reasonable reasons) much before that.
It is much easier to create object with a parameterless constructor through reflection, and then fill its properties with data through reflection, than to try and match data to arbitrary parameters of a parameterized constructor, with changing names/naming conflicts, undefined logic inside constructor, parameter sets not matching properties of an object, et cetera.
Many ORMs and serializers require parameterless constructors, because paramterized constructors through reflection are very fragile, and parameterless constructors provide both stability to the application and control over the object behavior to the developer.
Hibernate uses proxies for lazy loading. If you do no define a constructor or make it private a few things may still work - the ones that do not depend on proxy mechanism. For example, loading the object (with no constructor) directly using query API.
But, if you use session.load method() you'll face InstantiationException from proxy generator lib due to non-availability of constructor.
This guy reported a similar situation:
http://kristian-domagala.blogspot.com/2008/10/proxy-instantiation-problem-from.html
Check out this section of the Java language spec that explains the difference between static and non-static inner classes: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3
A static inner class is conceptually no different than a regular general class declared in a .java file.
Since Hibernate needs to instantiate ProjectPK independantly of the Project instance, ProjectPK either needs to be a static inner class, or declared in it's own .java file.
reference org.hibernate.InstantiationException: No default constructor
In my case, I had to hide my no-arg constructor, but because Hibernate I couldn't do it. So I solved the problem in another way.
/**
* #deprecated (Hibernate's exclusive constructor)
*/
public ObjectConstructor (){ }
Summarizing of what is below. It matters if you want to be JPA compatible or strictly Hibernate
Just look at official documentation: https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#entity-pojo
Section 2.1 The Entity Class of the JPA 2.1 specification defines its requirements for an entity class. Applications that wish to remain portable across JPA providers should adhere to these requirements:
One point says:
The entity class must have a public or protected no-argument
constructor. It may define additional constructors as well.
However, hibernate is less strict in this:
Hibernate, however, is not as strict in its requirements. The differences from the list above include:
One point says:
The entity class must have a no-argument constructor, which may be
public, protected or package visibility. It may define additional
constructors as well.
More on that is right below:
https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#entity-pojo-constructor
JPA requires that this constructor be defined as public or protected. Hibernate, for the most part, does not care about the constructor visibility, as long as the system SecurityManager allows overriding the visibility setting. That said, the constructor should be defined with at least package visibility if you wish to leverage runtime proxy generation.
I'm newly started reading about Java Beans and I had a question which was exactly same as this Topic's question. So I repeat The question:
in definition it is said "java bean encapsulates many objects into one object(Bean)."
1.What does "many objects" here mean?
and
2.How they are encapsulated into one object by java beans?
Edit:
From Java Beans Wikipedia:
in computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean).
Edit2:
all of classes have ability of having multiple attributes and fields.
If encapsulating of many objects means having multiple attributes and fields, I don't understand why they mentioned to this ability as a advantage of java bean class.
First to make it clear, every Class in Java extends the type Object. Something like String is also an Object.
The "many objects" is referring to how we can use different objects as fields within the bean. This creates a has-a relationship with the bean to your Objects.
For example, say we have this Bean:
public class YourBean implements java.io.Serializable {
private String s;
private ArrayList<String> list;
//Omitted rest of bean boilerplate
}
This example will contain two different Objects inside of it, the String s and the ArrayList<String> named list. You can add as many different Objects and primitives to your bean as you want.
To create the bean with a no-args constructor, you would then use:
YourBean bean = new YourBean();
And you can set and get the values of the Objects encapsulated within with:
ArrayList<String> yourList = new ArrayList<>();
bean.setList(yourList);
System.out.println(bean.getList());
You will be able to refer to all the Objects inside the bean this way by referencing the bean Object I named bean.
Additionally, you can create multiple of the same type of bean as well, so every time you make a new YourBean(), you will also be able to use all the Objects contained within.
This functionality is not unique to a Bean, you can do this in any Class, rather a Bean is a term used to describe a specific way you write some classes.
I recommend looking into Java Composition to learn when you should use a has-a relationship, rather than inheritance which is an is-a relationship.
We usually talk about Spring beans, but I think that's not what you're talking about. It seems to me that those JavaBeans are nothing but classes with multiple attributes and only getters/setters but whose constructor has zero arguments (and therefore it is mutable). As simple as that. The fact of encapsulating many objects is due to it having multiple attributes.
However, I have never referred to them as JavaBeans and I think the most similar concept I have ever worked with are the POJOs. I'm not sure if there is any difference, but the purpose looks the same.
If you ever talk about a bean in Java, I think anyone will think of a Spring bean. I suggest you not to use it in another context.
This is just my guessing. If I've said anything wrong please tell me.
JAVA Beans
The concept of JavaBeans was originally devised for Swing to facilitate the development of standalone GUI components, but the pattern has been repurposed for the land of Spring beans and back-end persistence with Hibernate
On the other hand, POJOs are simple java classes.
Another View:
Any POJO on having interaction with some third party become JAVA BEAN :)
Java Classes interacting with any ORM(say Hibernate)
Java Classes being used as Session Objects in EJB
As they say, "With Great Powers Comes Great Responsibilities" [excerpt from spiderman]
So our normal Pojos become JAVA BEANS :)
An article worth going through: https://mossgreen.github.io/Java-Bean-VS-Spring-Bean/
I know
A JavaBean is just a standard
All properties private (use getters/setters)
A public no-argument constructor
Implements Serializable.
Source
We all know it is not required to provide a non argument constructor in a class, because if we have not specified any constructor in our class java compiler will create a non argument constructor. If so why programmers wanted to create a non argument constructor in a javabean as a convention.
You are confusing requirements on the JavaBean class with the requirements on its source code. No part of the JavaBeans specification deals with how your source code must look like, it is strictly about the resulting class.
So yes, each JavaBeans class must provide a nullary constructor, and how you achieve that with source code (or even with a bytecode generator) is completely up to you.
It is considered good practice by some to always include the non-arg constructor in your code, because that prevents the scenario where a later maintenance introduces another constructor, thereby discarding the implicit non-arg one, thereby breaking any external code that relies on it.
You don't have to create it explicitly. There's no rule saying you have to do that. Even for a JavaBean, it's fine to leave the compiler to create one for you (as long as you're not providing another one, in which case you'd need an explicit no-arg constructor too).
But there does need to be one, explicit or implicit, because the ORM needs to be able to create instances.
You'd want to create a no argument in these cases:
1) You want to do some logic in the no argument constructor, so can't use the default.
2) You have other constructors that take arguments, in that case no default no-arg constructor will be provided for you.
point 2 implies that having an explicit no arg constructor to start with allows you to add future constructors with arguments without worrying about losing the implicit no-arg constructor.
Without one many API internals like ORMs or IOC containers can't instantiate the object in order to proceed with setting the bean properties from the data source or other bean dependencies.
Many do approximately this:
Class<?> clazz = Class.forName("com.foo.BeanClass");
Constructor<?> constructor = clazz.getConstructor();
Object bean = constructor.newInstance();
I understood, I think, that a "Bean" is a Java-class with properties and getters/setters.
As much as I understand, it is the equivalent of a C struct. Is that true?
Also, is there a real syntactic difference between a JavaBean and a regular class?
Is there any special definition or an Interface?
Basically, why is there a term for this?
Also what does the Serializable interface mean?
A JavaBean is just a standard. It is a regular Java class, except it follows certain conventions:
All properties are private (use getters/setters)
A public no-argument constructor
Implements Serializable.
That's it. It's just a convention. Lots of libraries depend on it though.
With respect to Serializable, from the API documentation:
Serializability of a class is enabled by the class implementing the
java.io.Serializable interface. Classes that do not implement this
interface will not have any of their state serialized or deserialized.
All subtypes of a serializable class are themselves serializable. The
serialization interface has no methods or fields and serves only to
identify the semantics of being serializable.
In other words, serializable objects can be written to streams, and hence files, object databases, anything really.
Also, there is no syntactic difference between a JavaBean and another class -- a class is a JavaBean if it follows the standards.
There is a term for it, because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the library requires your objects be proper JavaBeans).
There's a term for it to make it sound special. The reality is nowhere near so mysterious.
Basically, a "Bean":
is a serializable object (that is, it implements java.io.Serializable, and does so correctly), that
has "properties" whose getters and setters are just methods with certain names (like, say, getFoo() is the getter for the "Foo" property), and
has a public zero-argument constructor (so it can be created at will and configured by setting its properties).
As for Serializable: That is nothing but a "marker interface" (an interface that doesn't declare any functions) that tells Java that the implementing class consents to (and implies that it is capable of) "serialization" -- a process that converts an instance into a stream of bytes. Those bytes can be stored in files, sent over a network connection, etc., and have enough information to allow a JVM (at least, one that knows about the object's type) to reconstruct the object later -- possibly in a different instance of the application, or even on a whole other machine!
Of course, in order to do that, the class has to abide by certain limitations. Chief among them is that all instance fields must be either primitive types (int, bool, etc.), instances of some class that is also serializable, or marked as transient so that Java won't try to include them. (This of course means that transient fields will not survive the trip over a stream. A class that has transient fields should be prepared to reinitialize them if necessary.)
A class that can not abide by those limitations should not implement Serializable (and, IIRC, the Java compiler won't even let it do so.)
JavaBeans are Java classes which adhere to an extremely simple coding convention.
All you have to do is to
implement the java.io.Serializable interface - to save the state of an
object
use a public empty argument constructor - to instantiate the object
provide public getter/setter methods - to get and set the values of private variables (properties).
Properties of JavaBeans
A JavaBean is a Java object that satisfies certain programming conventions:
The JavaBean class must implement either Serializable or
Externalizable
The JavaBean class must have a no-arg constructor
All JavaBean properties must have public setter and getter methods
All JavaBean instance variables should be private
Example of JavaBeans
#Entity
public class Employee implements Serializable{
#Id
private int id;
private String name;
private int salary;
public Employee() {}
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
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;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
Explanation with an example.
1. import java.io.Serializable
As for the Serialization, see the documentation.
2. private fields
Fields should be private for prevent outer classes to easily modify those fields.
Instead of directly accesing to those fields, usuagly getter/setter methods are used.
3. Constructor
A public constructor without any argument.
4. getter/setter
Getter and setter methods for accessing and modifying private fields.
/** 1. import java.io.Serializable */
public class User implements java.io.Serializable {
/** 2. private fields */
private int id;
private String name;
/** 3. Constructor */
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
/** 4. getter/setter */
// getter
public int getId() {
return id;
}
public String getName() {
return name;
}
// setter
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
Java Beans are used for a less code and more work approach...
Java Beans are used throughout Java EE as a universal contract for runtime discovery and access. For example, JavaServer Pages (JSP) uses Java Beans as data transfer objects between pages or between servlets and JSPs. Java EE's JavaBeans Activation Framework uses Java Beans for integrating support for MIME data types into Java EE. The Java EE Management API uses JavaBeans as the foundation for the instrumentation of resources to be managed in a Java EE environment.
About Serialization:
In object serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
You will find serialization useful when deploying your project across multiple servers since beans will be persisted and transferred across them.
Just a little background/update on the bean concept. Many other answers actually have the what but not so much why of them.
They were invented early on in Java as part of building GUIs. They followed patterns that were easy for tools to pull apart letting them create a properties panel so you could edit the attributes of the Bean. In general, the Bean properties represented a control on the screen (Think x,y,width,height,text,..)
You can also think of it as a strongly typed data structure.
Over time these became useful for lots of tools that used the same type of access (For example, Hibernate to persist data structures to the database)
As the tools evolved, they moved more towards annotations and away from pulling apart the setter/getter names. Now most systems don't require beans, they can take any plain old Java object with annotated properties to tell them how to manipulate them.
Now I see beans as annotated property balls--they are really only useful for the annotations they carry.
Beans themselves are not a healthy pattern. They destroy encapsulation by their nature since they expose all their properties to external manipulation and as they are used there is a tendency (by no means a requirement) to create code to manipulate the bean externally instead of creating code inside the bean (violates "don't ask an object for its values, ask an object to do something for you"). Using annotated POJOs with minimal getters and no setters is much more OO restoring encapsulation and with the possibility of immutability.
By the way, as all this stuff was happening someone extended the concept to something called Enterprise Java Beans. These are... different. and they are complicated enough that many people felt they didn't understand the entire Bean concept and stopped using the term. This is, I think, why you generally hear beans referred to as POJOs (since every Java object is a POJO this is technically OK, but when you hear someone say POJO they are most often thinking about something that follows the bean pattern)
JavaBeans is a standard, and its basic syntax requirements have been clearly explained by the other answers.
However, IMO, it is more than a simple syntax standard. The real meaning or intended usage of JavaBeans is, together with various tool supports around the standard, to facilitate code reuse and component-based software engineering, i.e. enable developers to build applications by assembling existing components (classes) and without having to write any code (or only have to write a little glue code). Unfortunately this technology is way under-estimated and under-utilized by the industry, which can be told from the answers in this thread.
If you read Oracle's tutorial on JavaBeans, you can get a better understanding in that.
For a Java class to be usable as a Java bean, its method names need to be as per the JavaBeans guidelines (also called design patterns) for properties, methods, and events. The class needs to be a public class to be accessible to any beanbox tool or container. The container must be able to instantiate it; with the class as public, the container should be able to do so even if no explicit, public, zero-args constructor is provided. (A Java public class with no explicit constructor has a default public zero-args constructor.) So, minimally, a Java public class, even with a property as the sole member (of course, accompanying public getter and setter required) or a public method as the sole member, is a Java bean. The property can either be a read-only property (it has a getter method but no setter) or write-only property (has a setter method only). A Java public class with a public event listener registration method as the sole member is also a Java bean. The JavaBeans specification doesn’t require that if such a Java class has an explicit public constructor, it should be a zero-args one. If one could provide a file (with an extension, say, .ser) containing a serialized instance, a beanbox tool may be able to use that file to instantiate a prototype bean. Otherwise, the class would need a constructor, either explicit or default, that is public as well as zero-args.
Once the bean is instantiated, the JavaBeans API ( java.beans.*) can introspect it and call methods on it. If no class implementing the interface BeanInfo or extending a BeanInfo implementation,such as the SimpleBeanInfo class, is available, the introspection involves using reflection (implicit introspection) to study the methods supported by a target bean and then applying simple design patterns(the guidelines) to deduce from those methods what properties, events, and public methods are supported. If a class implementing the interface BeanInfo (for a bean Foo, it must be named FooBeanInfo) is available, the API bypasses implicit introspection and uses public methods (getPropertyDescriptor(), getMethodDescriptors(), getEventSetDescriptors() ) of this class to get the information. If a class extending SimpleBeanInfo is available, depending on which of the SimpleBeanInfo public methods (getPropertyDescriptor(), getMethodDescriptors(), getEventSetDescriptors() ) are overridden, it will use those overridden methods(s) to get information; for a method that is not overridden, it’ll default to the corresponding implicit introspection. A bean needs to be instantiated anyway, even if no implicit introspection is carried out on it. Thus, the requirement of a public zero-args constructor. But, of course, the Serializable or Externalizable interface isn’t necessary for it to be recognized. However, the JavaBeans specification says, ‘We’d also like it to be “trivial” for the common case of a tiny Bean that simply wants to have its internal state saved and doesn’t want to think about it.’ So, all beans must implement Serializable or Externalizable interface.
Overall, the JavaBeans specification isn’t hard and fast about what constitutes a bean. "Writing JavaBeans components is surprisingly easy. You don't need a special tool and you don't have to implement any interfaces. Writing beans is simply a matter of following certain coding conventions. All you have to do is make your class look like a bean — tools that use beans will be able to recognize and use your bean." Trivially, even the following class is a Java bean,
public class Trivial implements java.io.Serializable {}
The description so far is the Java SE version (JavaBeans). The beans, as described below, are the Java EE versions. These versions have been built on the underlying ideas as explained above. In particular, one main idea they consider is what if a bean constructor does have some parameters. These parameters could be either simple types, class/interface types or both. There should be a way to let the container know values that it can substitute for the parameters when instantiating the bean. The way to do so is that the programmer can configure (specify values) by say annotations or XML configuration files or a mix of both.
Spring Beans
Spring beans run in a Spring IoC container. The programmer can configure via XML configuration files, annotations or a mix of both.
In Spring, if a bean constructor has simple-type or class/interface type parameters, values can be assigned as strings (as the <value> attribute of a constructor argument element in the former case and as an <idref> element of a constructor argument in the latter case) in a type-safe manner. Making references to other Spring beans (called collaborators; via the <ref> element in a constructor argument element) is basically dependency injection and is also typesafe. Obviously, a dependency (collaborator bean) might have a constructor with injected parameters; those injected dependency(ies) might have a constructor with parameters and so on. This scenario should ultimately terminate at injected dependency(ies) that are prototype beans that the container can instantiate by constructing.
JSF Managed Beans
JSF managed beans run in a web container. They can be configured either with the #ManagedBean annotation or with an application configuration resource file managed-bean.xml. The JSF spec supports injection via resource injection (not typesafe) only. This injection is not fit for injection on constructors. In any case, the spec requires that a JSF managed bean must have a public zero-argument constructor. Further it says, “As of version 2.3 of this specification, use of the managed bean facility as specified in this section is strongly
discouraged. A better and more cohesively integrated solution for solving the same problem is to use Contexts and Dependency Injection (CDI), as specified in JSR-365." In other words, CDI managed beans should be used, which do offer typesafe dependency injection on constructors akin to Spring beans. The CDI specification adopts the Managed Beans specification, which applies to all containers of the JEE platform, not just the web tier. Thus, the web container needs to implement the CDI specification.
Managed Beans
Here is an extract from the Managed Bean specification
“ Managed Beans are container-managed objects with minimal requirements,
otherwise known under the acronym “POJOs” (Plain Old Java Objects)…they can be seen as a Java EE platform-enhanced version of the JavaBeans component model found on the Java SE platform…It won’t be missed by the reader that Managed Beans have a precursor in the homonymous facility found in the JavaServer Faces (JSF) technology…Managed Beans as defined in this specification represent a generalization of those found in JSF; in particular, Managed Beans can be used anywhere in a Java EE application, not just in web modules. For example, in the basic component model, Managed Beans must provide a no-argument constructor, but a specification that builds on Managed Beans, such as CDI (JSR-299), can relax that requirement and allow Managed Beans to provide constructors with more complex signatures, as long as they follow some well-defined rules...A Managed Bean must not be: a final class, an abstract class, or a non-static inner class. A Managed Bean may not be serializable unlike a regular JavaBean component.”
Thus, the specification for Managed Beans, otherwise known as POJOs or POJO beans, allows extension as in CDI.
CDI Beans
The CDI specification re-defines managed beans as:
When running in Java EE, a top-level Java class is a managed bean if it meets the requirements:
• It is not an inner class.
• It is a non-abstract class, or is annotated #Decorator.
• It does not implement javax.enterprise.inject.spi.Extension.
• It is not annotated #Vetoed or in a package annotated #Vetoed.
• It has an appropriate constructor, either: the class has a constructor with no parameters, or the class declares a constructor annotated #Inject.
All Java classes that meet these conditions are managed beans and thus no special declaration is
required to define a managed bean. Or
if it is defined to be a managed bean by any
other Java EE specification and if
• It is not annotated with an EJB component-defining annotation or declared as an EJB bean class
in ejb-jar.xml.
Bean constructors can have simple-type parameters since simple-types can be injected with the #Inject annotation.
EJBs
EJBs run in an EJB container. The EJB specification says: “A session bean component is a Managed Bean." “The class must have a public constructor that takes no arguments,” it says for both session bean and message-driven bean. Furthermore, it says, “The session bean class is not required to implement the SessionBean interface or the Serializable interface.” For the same reason as JSF beans, that EJB3 dependency injection is basically resource injection, JSF beans do not support constructors with arguments, that is, via dependency injection. However, if the EJB container implements CDI, “ Optionally: The class may have an additional constructor annotated with the Inject annotation, “ it says for both session bean and message-driven bean because, “An EJB packaged into a CDI bean archive and not annotated with javax.enterprise.inject.Vetoed annotation, is considered a CDI-enabled bean.”
As per the Wikipedia:
The class must have a public default constructor (with no arguments). This allows easy instantiation within editing and activation frameworks.
The class properties must be accessible using get, set, is (can be used for boolean properties instead of get), and other methods (so-called accessor methods and mutator methods) according to a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more than one argument.
The class should be serializable. (This allows applications and frameworks to reliably save, store, and restore the bean's state in a manner independent of the VM and of the platform.)
For more information follow this link.
Regarding the second part of your question, serialization is a persistence mechanism used to store objects as a sequence of signed bytes. Put less formally, it stores the state of an object so you can retrieve it later, by deserialization.
A Java Bean is a Java class (conceptual) that should follow the following conventions:
It should have a no-argument constructor.
It should be serializable.
It should provide methods to set and get the values of the properties, known as getter and setter methods.
It is a reusable software component. It can encapsulate many objects into one object so that same object can be accessed from multiples places and is a step towards easy maintenance of code.
They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java. According to Wikipedia.
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. According to Spring IoC.
It was repeated 6 or 7 times above that there is a no-argument constructor requirement for JavaBeans.
This is WRONG, there is no such requirement, especially in the context of Java Spring.
There is also no mention of that requirement in version (1.01) of the specification that describes the JavaBeanns APIs (https://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/). Even more - this specification mentions 'null constructor' only 2 times in the following contexts:
"Each customizer should have a null constructor."
"Each PropertyEditor should have a null constructor."
So, it does not seem like the authors of the spec don't know or are not willing to use the term "null constructor", still no mention of it for the JavaBeans proper.
A Java Bean is any Java class that satisfies the following three criteria:
It should implement the serializable interface (a Marker interface).
The constructor should be public and have no arguments (what other people call a "no-arg constructor").
It should have getter and setters.
Good to note the serialVersionUID field is important for maintaining object state.
The below code qualifies as a bean:
public class DataDog implements java.io.Serializable {
private static final long serialVersionUID = -3774654564564563L;
private int id;
private String nameOfDog;
// The constructor should NOT have arguments
public DataDog () {}
/** 4. getter/setter */
// Getter(s)
public int getId() {
return id;
}
public String getNameOfDog() {
return nameOfDog;
}
// Setter(s)
public void setId(int id) {
this.id = id;
}
public void setNameOfDog(String nameOfDog) {
this.nameOfDog = nameOfDog;
}}
If you are familiar with C/Golang, you never heard C bean or Go bean because they have struct keyword, that developers can easily define structure types without writing complicated OOP keywords.
type User struct {
Name string
Age int
}
var user User
user.Name = "name"
user.Age = 18
var bytes, err = json.Marshal(user)
It's Java's mistake that lack of struct types, and developers find this bad shortage.
Then Java Bean is invented as just another boring rule to make class pretending struct, peace your editor or compiler won't be crying or yelling about your unsafe access to class members.
To understand JavaBean you need to notice the following:
JavaBean is conceptual stuff and can not represent a class of specific things
JavaBean is a development tool can be visualized in the operation of reusable software components
JavaBean is based on the Sun JavaBeans specification and can be reusable components. Its biggest feature is the re-usability.
POJO (plain old Java object): POJOs are ordinary Java objects, with no restriction other than those forced by the Java Language.
Serialization: It is used to save state of an object and send it across a network. It converts the state of an object into a byte stream. We can recreate a Java object from the byte stream by process called deserialization.
Make your class implement java.io.Serializable interface. And use writeObject() method of ObjectOutputStream class to achive Serialization.
JavaBean class: It is a special POJO which have some restriction (or convention).
Implement serialization
Have public no-arg constructor
All properties private with public getters & setter methods.
Many frameworks - like Spring - use JavaBean objects.
If you want to understand Java-Beans, you first have to understand software-components.
Software components
A software-component is a part of an application that runs a specific operation. A software component can also be part of a service.
A component is:
Coupled (has dependencies)
Statefull (it saves the states of instance variables)
Not standarised, it is designed for a specific use case (main difference between Java-EE Beans)
Runs in client machine
Java Beans (Enterprise Beans)
Standarised components that run in a Java EE-server
Including different business logics to complete a specific service
Simplify development of complex multilayer distributed systems
Java Beans are more of a concept to manage big systems. Thats why they need standarization.
Source
In practice, Beans are just objects which are handy to use. Serializing them means to be able easily to persist them (store in a form that is easily recovered).
Typical uses of Beans in real world:
simple reusable objects POJO (Plain Old Java Objects)
visual objects
Spring uses Beans for objects to handle (for instance, User object that needs to be serialized in session)
EJB (Enterprise Java Beans), more complex objects, like JSF Beans (JSF is old quite outdated technology) or JSP Beans
So in fact, Beans are just a convention / standard to expect something from a Java object that it would behave (serialization) and give some ways to change it (setters for properties) in a certain way.
How to use them, is just your invention, but most common cases I enlisted above.
A Java Bean is a component or the basic building block in the JavaBeans architecture. The JavaBeans architecture is a component architecture that benefits from reusability and interoperability of a component-based approach.
A valid component architecture should allow programs to be assembled from
software building blocks (Beans in this case), perhaps provided by different vendors and also make it possible for an architect / developer to select a component (Bean), understand its capabilities, and incorporate it into an application.
Since classes/objects are the basic building blocks of an OOP language like Java, they are the natural contenders for being the Bean in the JavaBeans architecture.
The process of converting a plain Java class to a Java bean is actually nothing more than making it a reusable and interoperable component. This would translate into a Java class having abilities like:
controlling the properties, events, and methods of a class that are exposed to another application. (You can have a BeanInfo class that reports only those properties, events and methods that the external application needs.)
persistence (being serialisable or externizable - this would also imply having no-argument constructors, using transient for fields)
ability to register for events and also to generate events (e.g., making use of bound and constraint properties)
customizers (to customise the Bean via GUIs or by providing documentation)
In order for a Java class to be termed a Java bean it is not necessary that they need to possess all the above abilities. Instead, it implies to implement a subset of the above relevant to the context (e.g., a bean in a certain framework may not need customizers, some other bean may not need bound and constrained properties, etc.)
Almost all leading frameworks and libraries in Java adhere to the JavaBeans architecture implicitly, in order to reap the above benefits.
Spring #Bean annotation indicates that a method produces a bean to be managed by the Spring container.
More reference: https://www.concretepage.com/spring-5/spring-bean-annotation