The metadata of a bean has its own properties with independent bean definitions. Some of these bean definitions are as follows:
- Class: This will be used to create a bean, and it is mandatory to mention a class name for which we are supposed to create a bean.
- Name: If we want to define different aliases for the bean, then we use the name attribute, with the help of a separator, such as a comma (,) or semicolon (;). When we have XML-based configuration, we can use the name and/or id attribute as an identifier for a bean. A bean with an id attribute is preferred, because it is mapped with an actual XML ID element.
- Constructor-arg: A constructor argument is used to inject dependencies by passing a parameter as an argument in a constructor, which we saw in Chapter 3, Dependency Injection with Spring.
- Properties: We can directly pass properties with key-value pairs in a Spring bean for injecting. This is useful sometimes if we need to pass certain fixed values to a bean.
- Autowiring mode: Autowiring can be used to reduce the use of properties and constructor arguments. To enable autowiring mode, we need to use the autowire attribute in a Spring bean. Attributes can have values such as byName, byType, and constructor.
- Lazy initialization mode: By default, a Spring bean is created with a singleton scope, which initializes all the properties in eager mode. If a bean is defined with lazy mode, then an IOC container creates a bean instance the first time the request comes, rather than during the startup process.
- Initialization method: Spring initialization works after all the properties are set by an IOC container. In XML-based configuration, we can define an init method by defining an init-method attribute. The init method should be void, and without arguments. A @PostConstruct annotation can be used for initializing methods.
- Destruction method: On completion of a bean lifecycle, if we have to close resources or want to perform actions before destruction of a bean, we can use the destroy-method attribute of a bean in XML configuration. The @PreDestroy annotation is also used instead of the XML attribute.
The following configuration file contains different types of bean definition syntax and for that, the application-context.xml file could be:
<?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.xsd">
<!-- A simple bean definition with ID and Class Name-->
<bean id = "..." class = "...">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
<!-- Bean definition using Name attribute instead of ID attribute -->
<bean name = "..." class = "...">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
<!-- Ban definition with constructor arguments -->
<bean id="..." class="...">
<constructor-arg ref="..."/>
<constructor-arg ref="..."/>
</bean>
<!-- Ban definition for autowiring using byName mode -->
<bean id="..." class="..." autowire="byName">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
<!-- Ban definition for defining scope -->
<bean id="..." class="..." scope="prototype">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
<!-- Ban definition with lazy initialization mode -->
<bean id = "..." class = "..." lazy-init = "true">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
<!-- Bean definition which has initialization method -->
<bean id = "..." class = "..." init-method = "init">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
<!-- Bean definition which has destruction method -->
<bean id = "..." class = "..." destroy-method = "destroy">
<!-- Bean configuration and properties like constructor-arg -->
</bean>
</beans>
Lazy instantiation is most effective when the scope is singleton. With the prototype scope, a bean initializes with lazy mode by default.