Definition of Bean
the backbone that forms the application is the object that is managed by the spring IOC container, called the Bean. The bean is instantiated, assembled, and managed by the spring IOC container. These beans are provided by the container, for example, in the <bean/> definition of XML, which has been seen in the form of the previous chapters to configure metadata creation.
The bean definition contains the container you need to know the following information called configuration metadata:
- How to create a bean
- More information about the Bean life cycle
- Bean dependencies
All of the above configuration metadata is converted into a group of the following properties to form the definition of each bean.
Spring Configuration meta Data
The Spring IOC container is decoupled entirely from the format actually written by this configuration metadata. There are three important methods for the spring container that provides the following configuration metadata:
- XML-based configuration files
- Annotation-based configuration
- Java-based configuration
We have seen how xml-based configuration metadata is provided to the container, but let us see different bean definitions, including deferred initialization, initialization methods, and another example of the destruction method based on an XML configuration file:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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--> <bea n id= "class=" ... "> <!--Collaborators and configuration for this bean go here--> </bean> ;! --A bean definition with lazy init set on--> <bean id= "..." class= "true" > <!--lazy-init= Rators and configuration for the This bean down--> </bean> <!--A Bean definition with initialization met Hod--> <bean id= "class=" ... "init-method=" ... "> <!--Collaborators and configuration for this bean Go to--> </bean> <!--A Bean definition with destruction method--> <bean id= "..." class= "... . "Destroy-method=" ... "> ≪! --Collaborators and configuration for the this bean to go to--> </bean> <!--more beans definitions Go-
-> </beans>
The life cycle of a bean
the life cycle of the Spring bean is easy to understand. When a bean is instantiated, it may need to perform some initialization to convert it to a usable state. Similarly, some cleanup work may need to be done when the bean is no longer needed and is removed from the container.
There are, however, activities that take place between the instantiation of the bean and the time of destruction, but this chapter will discuss only two of the life cycle callback methods of the important bean that are needed when the bean is initialized and destroyed.
To define the installation and disassembly of a bean, we simply declare the initialization method and/or destroy the method parameter <bean>. Specifies a method in the Init-method property that is instantiated immediately after the bean is invoked. Similarly, the destruction method prescribes the method that is invoked before the bean is removed from the container.
Initialize callback:
The Org.springframework.beans.factory.InitializingBean interface specifies a single method:
void Afterpropertiesset () throws Exception;
Therefore, you can simply implement the above interface and initialization work can be in the inside Afterpropertiesset () method, as follows:
public class Examplebean implements Initializingbean {public
void Afterpropertiesset () {
//do some Initializati On work
}
}
In the case of xml-based configuration metadata, you can use the Init-method property to specify the name of a method with void parameterless signatures. For example:
<bean id= "Examplebean"
class= "examples". Examplebean "init-method=" Init "/>
The following is the definition of a class:
public class Examplebean {public
void init () {
//Do some initialization work
}
}
Destroy callback
The Org.springframework.beans.factory.DisposableBean interface specifies a single method:
void Destroy () throws Exception;
Therefore, you can simply implement the above interface and the final work can be done inside the Destroy () method, as follows:
public class Examplebean implements Disposablebean {public
void Destroy () {
//Do some destruction work
c15/>}
In the case of xml-based configuration metadata, you can use the Destroy-method property to specify the name of a method with void parameterless signatures. For example:
<bean id= "Examplebean"
class= "examples". Examplebean "destroy-method=" Destroy "/>
The following is the definition of a class:
public class Examplebean {public
void Destroy () {
//Do some destruction work
}
}
If you are using Spring's IOC container in a non-web application environment, for example, in a desktop rich-client environment; Register to close the hook in the JVM. Doing so ensures that the shutdown is normal and that all resources are freed to invoke the associated destroy method on the singleton bean.
It is not recommended to use Initializingbean or Disposablebean callbacks because XML configuration provides great flexibility in naming your methods.
For example:
Use the Eclipse IDE, and then follow these steps to create a spring application: