Spring Tutorial - Bean Definition

Beans are the building block of any application. Objects are created based on Beans. Beans are the objects that are the building block of an application and are managed by IoC container.

In the previous chapter ( Spring tutorial – IoC Container ), we have seen that how IoC container instantiates, assemble, and manage the bean objects.

Bean Definition contains the information known as configuration metadata, configuration metadata is required for the container. It takes care of:

        1.     How to create the bean?

        2.     Track bean’s life cycle details

        3.     Manages bean dependencies.

Each bean definition contains below-following properties:

        1.     Name – this attribute specifies the bean uniquely. In XML based configuration metadata either name or id is used to specify bean uniquely.

        2.     class – This attribute specifies the bean class to be used by configuration metadata to create a bean object.

        3.     scope – this attribute specifies the bean’s object scope. The bean’s object will be limited to the specified scope. We will discuss the Bean Scope topic in the next chapter.

        4.     constructor-arg – this attribute is used to pass arguments to the constructor.

        5.     properties – This attribute is also used to inject dependencies into the bean.

        6.     auto wiring mode – this attribute also injects the dependencies. Autowiring is injecting a bean into another bean. This we will be discussing the further chapters.

        7.     Lazy-initialization mode – this attribute specifies the IoC container to load the bean instance as soon as it is created rather than at startup.

        8.     initialization method – this attribute specifies the initialization method which will be called at the time of bean initialization.

        9.     destruction method – this attribute specifies the destroy method which will be called at the time of bean destruction.

Spring Configuration Metadata

There are three ways in which we can provide configuration metadata to the Spring container:

        1.     XML based configuration file.

        2.     Annotation-based configuration.

        3.     Java-based configuration.

We have already seen the XML based configuration, below is one more example which uses some more properties mentioned above:

For more details on how to define spring bean, its configuration and creation, you can check out “Spring tutorial – Hello World Example

Besides XML based there is another way to inject the dependencies which are annotation based. Which we will discuss in the further chapter. But before understanding annotation-based configuration there are few concepts that we need to understand.

In the next chapter, we will discuss “Spring tutorial – Bean Scopes”.

Post a Comment

0 Comments