init Method in Spring
Calling Bean using init() method in Spring, this section describes the way to initialize a
bean through its lifecycle events using the init() method .Here we have
defined the property and values of the bean using the init method as shown
below:- i
<bean id="mybean" class="Bean" init-method="init">:-Here
"Bean" is the name of the bean class which would be referred in
the xml file with the id "mybean".
init-method="init":-Specify the init method named
"init" in the configuration file.
<property name="name"> <value>Roseindia.net</value>:-Here
the <property> element is used to declare the attributes and to pass the desired value to the
property element, the <value> element is used. Here the property name
is "name"and its value is "Roseindia.net".
<bean id="mybean" class="Bean" init-method="init">
<property name="name">
<value>Roseindia.net</value>
</property>
<property name="address">
<value>Rohini</value>
</property>
<property name="type">
<value>Software Development Company</value>
</property>
</bean>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
|
Here is the file named SimpleBean.java through which we are retrieving the properties of the bean which we have defined in the above file i.e. initMethod
.xml.BeanFactory factory = new XmlBeanFactory(new FileSystemResource("initMethod.xml")):-Creates an instance of the XmlBeanFactory which is used to read bean definition from an XML document.
SimpleBean.java
import org.springframework.beans.factory.BeanCreationException;
|
Output of the program
|
Initializing bean
Name: Roseindia.net Address: Rohini Type: Software Development Company |