Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring Bean definition


May 14, 2021 Spring


Table of contents


Bean definition

Objects called beans are the backbone of the application and are managed by the Spring IoC container. a bean is an object that is instantiated, assembled, and managed through a Spring IoC container. These beans are created from configuration metadata provided in containers, for example, as seen in previous sections, defined in XML forms.

The bean definition contains information called configuration metadata, and the following containers also need to know the configuration metadata:

  • How to create a bean

  • Details of the life cycle of the bean

  • The dependency of the bean

All of the above configuration metadata is converted into a set of the following properties that make up each bean definition.

Property Describe
class This property is mandatory and specifies the bean class used to create the bean.
name This property specifies a unique bean identifier. In XML-based configuration metadata, you can specify bean identifiers using the ID and/or name properties.
scope This property specifies the scope of the object created by the specific bean definition, which is discussed in the section on the bean scope.
constructor-arg It is used to inject dependencies and will be discussed in the next chapter.
properties It is used to inject dependencies and will be discussed in the next chapter.
autowiring mode It is used to inject dependencies and will be discussed in the next chapter.
lazy-initialization mode The delayed initialization of the bean tells the IoC container to create a bean instance the first time it is requested, rather than at startup.
Initialization method The callback method is called after all the required properties of the bean have been set by the container. It will be discussed in the lifecycle section of the bean.
The destruction method Use the callback method when the container containing the bean is destroyed. It will be discussed in the lifecycle section of the bean.

The relationship between bean and the Spring container

The following illustration shows the relationship between Bean and the Spring container:

Spring Bean definition

Spring configuration metadata

The Spring IoC container is completely decoupled from the format of the configuration metadata that is actually written. There are three important ways to provide configuration metadata to the Spring container:

  • XML-based profiles

  • Annotation-based configuration

  • Java-based configuration

Tip: For XML-based configurations, Spring 2.0 later uses Schema's format, making different types of configurations have their own namespaces and making profiles more scalable.

You've seen how XML-based configuration metadata can be made available to containers, but let's look at another example of an XML-based profile with different bean definitions, including deferred initialization, initialization methods, and destruction methods:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- A simple bean definition -->
   <bean id="..." class="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with lazy init set on -->
   <bean id="..." class="..." lazy-init="true">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with initialization method -->
   <bean id="..." class="..." init-method="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with destruction method -->
   <bean id="..." class="..." destroy-method="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- more bean definitions go here -->

</beans>

In the example above:

(1) xmlns " http://www.springframework.org/schema/beans", default namespace: it does not have a space name for the definition of Spring Bean;

(2) xmlns:xsi-"http://www.w3.org/2001/XMLSchema-instance", xsi namespace: This namespace is used to specify the appropriate Schema style file for each document namespace, which is the standard namespace defined by the standard organization.

You can view the Spring Hello World instance to learn more about how to define, configure, and create Spring Beans.

The annotation-based configuration is discussed in a separate section. I deliberately left it in a separate section because I wanted you to master some other important Spring concepts before you started using annotations and Spring dependency injection programming.