60 seconds to spring (2)

zhaozj2021-02-12  183

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 Thatorial 2 Will DEMOSTRATE The Difference Behaviour When Defining a bean as singleton and prototype.

Note: by Default All Beans Are Deployed in Singleton Mode, UNSS you specific Otherwise.

Step 1: Create a counterbean.java in SRC

ORG / JARCHITECT / SPRING / TUTORIAL2 / CounterBean.java

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 counterboan class in the bean.xml

Do Notice That I Have Defined One Counterbean As Singleton Bean and The Other AS Prototype Bean.

Jarchitect Spring Tutorial 2

Step 3: Create a main.java in SRC

Org / jarchitect / spring / tutorial2 / main.java

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 = ("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 = ("prototypebean); system.out.println (" first call: - " prototypeever.getcount PrototypeBean = (Counterbean) Factory.getBean; 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 is 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 Secondsdone.

[TOC]

转载请注明原文地址:https://www.9cbs.com/read-6818.html

New Post(0)