In the previous article, we have discussed section 3.2.4 of Spring framework development reference manual, bean identifier (ID and name), and continue section 3.2.5, Singleton usage or not. The document has clearly explained Singleton, and it is not nonsense here. We mainly know that the default deployment method in spring is Singleton. If there is no special need, Singleton is generally used. For software configuration, see Quick Start spring -- 2. helloworld (1). Take hellobean as an example. All three files are stored in the javamxj. Spring. Basic. Singleton package. · Hellobean adds attribute I here and uses it to obtain random numbers.
| Hellobean. Java |
| Package javamxj. Spring. Basic. Singleton; Public class hellobean { Private string helloworld; Private int I = (INT) (100 * Math. Random ()); Public hellobean (string helloworld ){ This. helloworld = helloworld; } Public void sayhello (){ System. Out. println (helloworld ); System. Out. println ("output random INTEGER:" + I ); } } |
· In this beans, the configuration file defines two beans pointing to the hellobean class. The main difference is that the second bean specifies
Singleton = "false", That is, the singleton deployment method is not used.
| Bean. xml |
<? XML version = "1.0" encoding = "GBK"?> <! Doctype beans public "-// spring/DTD bean/EN" Http://www.springframework.org/dtd/spring-beans.dtd><Beans> <Bean id = "singletonbean" class = "javamxj. Spring. Basic. Singleton. hellobean"> <Constructor-Arg> <Value> hello! This is singletonbean! </Value> </Constructor-Arg> </Bean>
<Bean id = "prototypebean" class = "javamxj. Spring. Basic. Singleton. hellobean" Singleton ="False"> <Constructor-Arg> <Value> hello! This is prototypebean! </Value> </Constructor-Arg> </Bean>
</Beans> |
· Test program: Obtain singletonbean and prototypebean instances respectively, output random numbers, and compare the references of the two instances of the same bean.
| Main. Java |
| Package javamxj. Spring. Basic. Singleton; Import org. springframework. Beans. Factory. beanfactory; Import org. springframework. Beans. Factory. xml. xmlbeanfactory; Import org. springframework. Core. Io. classpathresource; Import org. springframework. Core. Io. resource; Public class main { Public static void main (string [] ARGs ){ Resource res = new classpathresource ("javamxj/spring/basic/Singleton/bean. xml "); Beanfactory factory = new xmlbeanfactory (RES ); Hellobean H1 = (hellobean) Factory. getbean ("singletonbean "); H1.sayhello ();
Hellobean H2 = (hellobean) Factory. getbean ("singletonbean "); H2.sayhello ();
System. Out. println ("h1 = h2:" + (H1 = h2 )); System. Out. println ("");
Hellobean h3 = (hellobean) Factory. getbean ("prototypebean "); H3.sayhello ();
Hellobean h4 = (hellobean) Factory. getbean ("prototypebean "); H4.sayhello ();
System. Out. println ("h3 = H4:" + (h3 = h4 )); } } |
· Run, console output:
Hello! This is singletonbean!
Output random INTEGER: 7
Hello! This is singletonbean!
Output random INTEGER: 7
H1 = h2: True
Hello! This is prototypebean!
Output random INTEGER: 95
Hello! This is prototypebean!
Output random INTEGER: 60
H3 = H4: false· Download the source code of this article and the previous article (excluding library files): http://free.ys168.com /? Springbasic.zip in the spring directory of javamxj.