I am yet to find a high-level definition of Spring beans that I can understand. I see them referenced often in Grails documentation and books, but I think that understanding
First let us understand Spring:
Spring is a lightweight and flexible framework.
Analogy:
Bean: is an object, which is created, managed and destroyed in Spring Container. We can inject an object into the Spring Container through the metadata(either xml or annotation), which is called inversion of control.
Analogy: Let us assume farmer is having a farmland cultivating by seeds(or beans). Here, Farmer is Spring Framework, Farmland land is Spring Container, Beans are Spring Beans, Cultivating is Spring Processors.
Like bean life-cycle, spring beans too having it's own life-cycle.
img source
Following is sequence of a bean lifecycle in Spring:
Instantiate: First the spring container finds the bean’s definition from the XML file and instantiates the bean.
Populate properties: Using the dependency injection, spring populates all of the properties as specified in the bean definition.
Set Bean Name: If the bean implements BeanNameAware
interface, spring passes the bean’s id to setBeanName()
method.
Set Bean factory: If Bean implements BeanFactoryAware
interface, spring passes the beanfactory to setBeanFactory()
method.
Pre-Initialization: Also called post process of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization()
method.
Initialize beans: If the bean implements IntializingBean
,its afterPropertySet()
method is called. If the bean has init method declaration, the specified initialization method is called.
Post-Initialization: – If there are any BeanPostProcessors
associated with the bean, their postProcessAfterInitialization()
methods will be called.
Ready to use: Now the bean is ready to use by the application
Destroy: If the bean implements DisposableBean
, it will call the destroy()
method
Spring beans are classes. Instead of instantiating a class (using new
), you get an instance as a bean
cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.
In terms of a Spring boot application, a bean is simply a Java object which is created by Spring framework when the application starts.
The purpose of the object can be pretty much anything - a configuration, a service, database connection factory etc. - Spring doesn't really care.
Most beans depend on other beans to work, for example an entity manager might need a database connection. Spring framework is able to figure out how the beans should be wired together automatically. From your point of an application developer, you just have to declare the beans you need and they "magically" appear in your application ready to use.
Spring beans are just instance objects that are managed by the Spring container, namely, they are created and wired by the framework and put into a "bag of objects" (the container) from where you can get them later.
The "wiring" part there is what dependency injection is all about, what it means is that you can just say "I will need this thing" and the framework will follow some rules to get you the proper instance.
For someone who isn't used to Spring, I think Wikipedia Spring's article has a nice description:
Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together.
Objects created by the container are also called managed objects or beans. The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. These data sources contain the bean definitions which provide the information required to create the beans.
Objects can be obtained by means of either dependency lookup or dependency injection. Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type. Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods.
Spring beans are just object instances that are managed by the Spring IOC container.
Spring IOC container carry the Bag of Bean.Bean creation,maintain and deletion are the responsibilities of Spring Container.
We can put the bean in to Spring by Wiring and Auto Wiring.
Wiring mean we manually configure it into the XML file.
Auto Wiring mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring.
Spring have the IoC container which carry the Bag of Bean ; creation maintain and deletion are the responsibilities of Spring Container. We can put the bean in to Spring by Wiring and Auto Wiring. Wiring mean we manually configure it into the XML file and "Auto Wiring" mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring.
Here is the detail URI where you got more information about Beans