Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

 

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:-

<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>

initMethod.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"
>
<beans>
    <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>
</beans>

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;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Bean {

    private static final String DEFAULT_NAME = "Girish";
    private String name = null;
    private String address = null;
    private String type = null;
  

    public void setName(String name) {
        this.name = name;
    }
  public void setAddress(String address) {
        this.address = address;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void init() {
        System.out.println("Initializing bean");

        if (name == null) {
            System.out.println("Using default name");
            name = DEFAULT_NAME;
        }
        if (address == null) {
            System.out.println("Using default name");
            address = DEFAULT_NAME;
        }
        if (type == null) {
            System.out.println("Using default name");
            type = DEFAULT_NAME;
        }
       
    }
    public String toString() {
        return "Name: " + name + "\nAddress: " + address + "\nType: " + type ;
    }

    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
                "initMethod.xml"));

        Bean Bean1 = getBean("mybean", factory);
    }
    private static Bean getBean(String beanName, BeanFactory factory) {
        try {
            Bean bean = (Beanfactory.getBean(beanName);
            System.out.println(bean);
            return bean;
        catch (BeanCreationException ex) {
            System.out.println("An error occured 
            in bean configuration: " + ex.getMessage());
            return null;
        }
    }
}  

Output of the program

Initializing bean

Name: Roseindia.net

Address: Rohini

Type: Software Development Company

Download Source code

                         
» View all related tutorials
Related Tags: c spring development framework application io enterprise read applications app frame for work ram erp e li dev enter in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.