img JasonSeaver

60 seconds to Spring (2)

发表于2004/7/6 23:25:00  644人阅读

分类: Framework

60 sec to Spring [TOC]

Bean Tutorial 2 - To Singleton or Not To Singleton

Beans are defined to be deployed in one of the two modes: Singleton and Non-Singleton (Prototype). When a bean is a singleton, only one shared instance of the bean will be managed. The prototype mode of a bean deployment results in the creation of a new bean instance every time a request for that specific bean is done. Tutorial 2 will demostrate the difference behaviour when defining a bean as Singleton and Prototype.

Note: By default all beans are deployed in singleton mode, unless you specify otherwise.

Step 1: Create a CounterBean.java in src


package org.jarchitect.spring.tutorial2; public class CounterBean { private int counter; public CounterBean() { System.out.println("Construct CounterBean......"); } public int getCount() { return counter++; } } 

Step 2: Specify the CounterBean class in the bean.xml

Do notice that I have defined one CounterBean as singleton bean and the other as prototype bean.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <description>jarchitect Spring Tutorial 2</description> <bean id="singletonBean" class="org.jarchitect.spring.tutorial2.CounterBean" singleton="true"/> <bean id="prototypeBean" class="org.jarchitect.spring.tutorial2.CounterBean" singleton="false"/> </beans>

Step 3: Create a Main.java in src


package org.jarchitect.spring.tutorial2; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import org.springframework.beans.factory.xml.XmlBeanFactory; public class Main { public static void main(String[] args) { try { //Read the configuration file InputStream is = new FileInputStream("bean.xml"); XmlBeanFactory factory = new XmlBeanFactory(is); //Instantiate an singleton object System.out.println("---- Singleton Bean ----"); CounterBean singletonBean = (CounterBean) factory.getBean("singletonBean"); System.out.println("First Call :- " + singletonBean.getCount()); singletonBean = (CounterBean) factory.getBean("singletonBean"); System.out.println("Second Call :- " + singletonBean.getCount()); System.out.println(""); //Instantiate an object System.out.println("---- Prototype Bean ----"); CounterBean prototypeBean = (CounterBean) factory.getBean("prototypeBean"); System.out.println("First Call :- " + prototypeBean.getCount()); prototypeBean = (CounterBean) factory.getBean("prototypeBean"); System.out.println("Second Call :- " + prototypeBean.getCount()); } catch (FileNotFoundException e) { e.printStackTrace(); } } }

Step 4: Use an ANT script to build and execute the Main class. Just run ant from the command prompt will do the trick.

Below are the output from ANT.

C:/60sec2Spring/SpringTutorial2>ant Buildfile: build.xml build: [javac] Compiling 2 source files to C:/60sec2Spring/SpringTutorial2/bin run: [java] Jun 1, 2004 12:16:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions [java] INFO: Loading XML bean definitions from (no description) [java] Jun 1, 2004 12:16:42 PM org.springframework.beans.factory.support.AbstractBeanFactory getBean [java] INFO: Creating shared instance of singleton bean 'singletonBean' [java] ---- Singleton Bean ---- [java] Construct CounterBean...... [java] First Call :- 0 [java] Second Call :- 1 [java] ---- Prototype Bean ---- [java] Construct CounterBean...... [java] First Call :- 0 [java] Construct CounterBean...... [java] Second Call :- 0 BUILD SUCCESSFUL Total time: 2 seconds



0 0



取 消