Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JavaBean and Spring bean

I am new to Spring MVC and have a little idea of the usage of java beans in Java.
What is the basic difference between a Java bean and Spring bean?

like image 644
Arpit Shah Avatar asked Feb 18 '14 22:02

Arpit Shah


People also ask

What is JavaBean How does a bean differ from a class?

JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a java class that should follow following conventions: Must implement Serializable. It should have a public no-arg constructor.

What is JavaBean used for?

JavaBeans is a portable, platform-independent model written in Java Programming Language. Its components are referred to as beans. In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It helps in accessing these object from multiple places.

Are Spring beans POJO?

POJO means Plain Old Java Object. It refers to a Java object that isn't bogged down by framework extensions. We associate Spring with POJO to express that with Spring, the beans stay simple, testable, adaptable, etc..., not or rather few coupled to specific framework interfaces or implementations.

Why is it called JavaBean?

actually when they were developing java , the developers consumed so much of coffee so they made it as their symbol. and then so as the beans are small parts of the coding they named it as beans corresponding to small coffee beans. Yes, that's right.


2 Answers

JavaBeans:

At a basic level, JavaBeans are simply Java classes which adhere to certain coding conventions. Specifically, classes that

  • have public default (no argument) constructors
  • allow access to their properties using accessor (getter and setter) methods
  • implement java.io.Serializable

Spring Beans:

A Spring bean is basically an object managed by Spring. More specifically, it is an object that is instantiated, configured and otherwise managed by a Spring Framework container. Spring beans are defined in Spring configuration files (or, more recently, with annotations), instantiated by Spring containers, and then injected into applications.

Note that Spring beans need not always be JavaBeans. Spring beans might not implement the java.io.Serializable interface, can have arguments in their constructors, etc.

This is the very basic difference between JavaBeans and Spring beans.

For more information, refer to the source of the above text, Shaun Abram's article JavaBeans vs Spring beans vs POJOs.

like image 120
keya Avatar answered Oct 19 '22 11:10

keya


Java bean is a class that should follow following conventions:

1.Must implement Serializable. 2.It should have a public no-arg constructor. 3.All properties in java bean must be private with public getters and setter methods.

Spring beans are the objects that form the backbone of your application and are managed by the Spring IoC container .

like image 42
Hamid hamid Avatar answered Oct 19 '22 13:10

Hamid hamid