Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

Create/Destroy the Beans Globally


All the complex applications use the design pattern these days. So if any one want to use the same createInstance method for all the beans creations in the application, then he needs to unnecessary copy the same factory-method="createInstance" in all the bean definitions. Spring provide a better solution for the same. You can use "default-destroy-method" and "default-init-method" attributes of the <beans> tag. The below example will explain the use of both attributes. You can also override the method names for some beans if you want by introducing the factory-method method and it is still valid.


Example To use the Factory method Instantiation in Spring:-


TestBean.java









package com.visualbuilder.beans;


public class TestBean {



private static TestBean bean = null;



    public String toString(){

        return "Bean has been called";

        }

    public static TestBean createInstance(){

        if(bean == null) {

                        bean = new TestBean();

            System.out.println("Bean is created");

        }



    return bean;

    }

}



Spring-beans.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-2.5.xsd" default-destroy-method="destroy" default-init-method="createInstance">

<bean id="test" class="com.visualbuilder.beans.TestBean"/>


</beans>



 


Output:- On running the ApplicationContextExample.java file we got the following output.


Jun 1, 2008 1:49:48 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Jun 01 13:49:48 IST 2008]; root of context hierarchy

Jun 1, 2008 1:49:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Bean is created

Jun 1, 2008 1:49:48 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9

Jun 1, 2008 1:49:48 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9: defining beans [test]; root of factory hierarchy

Bean has been called

                    

Copyright © 2012 VisualBuilder. All rights reserved