What in the world are Spring beans?

Issue

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 what they are would be beneficial. So what are Spring beans? How can they be used? Do they have something to do with Dependency Injection?

Solution

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. These beans are created with the configuration
metadata that you supply to the container, for example, in the form of
XML <bean/> definitions.

More to learn about beans and scope from SpringSource:

When you create a bean definition what you are actually creating is a
recipe for creating actual instances of the class defined by that bean
definition. The idea that a bean definition is a recipe is important,
because it means that, just like a class, you can potentially have
many object instances created from a single recipe.

You can control not only the various dependencies and configuration
values that are to be plugged into an object that is created from a
particular bean definition, but also the scope of the objects created
from a particular bean definition. This approach is very powerful and
gives you the flexibility to choose the scope of the objects you
create through configuration instead of having to ‘bake in’ the scope
of an object at the Java class level. Beans can be defined to be
deployed in one of a number of scopes

*IoC: Inversion of Control

Answered By – Juned Ahsan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published