## 三V工作室·泰伯子仪

http://www.sanv.org/

# 60 seconds to Spring （2）

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

org/jarchitect/spring/tutorial2/CounterBean.java

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.

 

Step 3: Create a Main.java in src

org/jarchitect/spring/tutorial2/Main.java

  org.jarchitect.spring.tutorial2; }

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.

 

Done.

05-14 2207

06-16 1.1万

01-12 657

12-01 312

11-01 1269

02-12 9724

07-06 799

07-06 807

09-12 703

12-01 523