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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Bean life cycle in spring 
 

This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring.

 

Bean life cycle in spring

                         

This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring. Run the given bean example to retrieves the values of the bean using java file. Here in the file given below i.e. (context.xml) we have declare the bean definition.

<bean id="Mybean" class="Bean">         
         <property name="company" value="Name"/>         <property name="value" value="Roseindia.net"/>
        </bean>

Here "Bean" is the name of the bean class which would be further referred in the xml file with the id "MyBean".

<property name="company" value="Name"/>:-Declares  the property name of the bean and its value.

context.xml

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

    <bean id="Mybean" class="Bean">
        <property name="company" value="Name"/>
        <property name="value" value="Roseindia.net"/>   
    </bean>
</beans>

Here is the file named Main.java through which we are retrieving the properties of the bean which we have defined in the above file i.e. context.xml

 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-Here we are creating an instance of the XmlBeanFactory which is used to read bean definition from an XML document

new ClassPathResource("context.xml"):-
Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.

class BeanSupport implements InitializingBean :-Here the InitializingBean  interface is implemented by bean class. The use of this interface here is  to do some post processing actions when all the properties have been set by the Bean Factory..

 @Override     
public String toString() {         return String.format("%s : \"%s\"", this.company, getValue());   
  }

Here the method toString() is overridden which is returning the the company name and value that has been defined in the context.xml file.

Main.java

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {

    public static void main(String[] argsthrows Exception {
     XmlBeanFactory factory = new XmlBeanFactory(new 
                              ClassPathResource("context.xml"));

     System.out.println(factory.getBean("Mybean"));
    }
}
class Bean extends BeanSupport {

    private String company;
        
    public void setCompany(String company) {
        this.company = company;
    }
    @Override
    public String toString() {
        return String.format("%s : \"%s\""this.company, getValue());
    }
}
class BeanSupport implements InitializingBean {

    private String value;

    public final void afterPropertiesSet() throws Exception {
    }

    public final void setValue(String value) {
        this.value = value;
    }

    protected final String getValue() {
        return this.value;
    }
}

Output of the program

Nov 25, 2008 5:25:41 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Name : "Roseindia.net"
BUILD SUCCESSFUL (total time: 1 second)

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.