This article describes the lookup method injection in section 3.3.3.1 of Spring framework development reference manual. Take a closer look at the document. This method is mainly used to obtain non-singleton beans by using the lookup-method in singleton objects. Generally, it is not used much. You 'd better understand your needs before using this definition. ·
First create a package: javamxj. Spring. Basic. lookup, and then put the following five files under this package.
Hello. java. |
Package javamxj. Spring. Basic. lookup; Public interface Hello { Public random getrandom ();
Public Abstract Random Createrandom (); } |
Random. Java |
Package javamxj. Spring. Basic. lookup; Public class random { Private int I = (INT) (100 * Math. Random ()); Public void printrandom (){
System. Out. println ("output random INTEGER:" + I );
} }
|
Helloabstract. Java |
Package javamxj. Spring. Basic. lookup; Public abstract class helloabstract implements Hello { Private random; Public random getrandom (){
Return random;
} Public void setrandom (random ){
This. Random = Random; } Public Abstract Random Createrandom (); } |
Beans. 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 = "ran" Class = "javamxj. Spring. Basic. lookup. Random" Singleton = "false"/>
<Bean id = "hello" Class = "javamxj. Spring. Basic. lookup. helloabstract"> <Lookup-method name = "createrandom" Bean = "ran"/> <Property Name = "random"> <Ref Local = "ran"/> </Property> </Bean>
</Beans> |
Package javamxj. Spring. Basic. lookup;
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/lookup/beans. xml ");
Beanfactory Ft = new
Xmlbeanfactory (RES );
Hello H = (Hello) ft. getbean ("hello ");
Random R1 =
H. getrandom ();
Random r2 =
H. getrandom ();
System. Out. println ("The lookup method is not used for injection :");
System. Out. println ("the two random instances point to the same reference:" + (r1 = R2 ));
R1.printrandom ();
R2.printrandom ();
Random R3 =
H. createrandom ();
Random r4 =
H. createrandom ();
System. Out. println ("/n uses the lookup Method for injection :");
System. Out. println ("the two random instances point to the same reference:" + (R3 = R4 ));
R3.printrandom ();
R4.printrandom ();
}
}
Brief description:
· Hello is an interface class that implements interface-oriented programming. · The random class is used to output random integers. ·
Helloabstract is an abstract class that contains an attribute: Random and an abstract method createrandom (). If this method is not abstract, spring will rewrite the existing implementation. ·
Beans. xml defines two beans. The ran points to the rondom class, note that it is not Singleton; Hello points to the helloabstract class, where the random attribute points to ran, and the createrandom method also points to ran. ·
In the main class, the Hello class uses the getrandom () and createrandom () Methods to call the random class. ·
The main directory of spring-framework/lib/cglib is required this time.
DirectoryCglib-nodep-2.1_2.jarAdd to project
LibrariesTo use the dynamic proxy.
Running result:
The lookup method is not used for injection:
The two random instances point to the same reference: True
Random integer output:
98
Output random INTEGER: 98 is injected using the lookup method:
Two random instances direct to the same reference: false
Random integer output:
51
Output random INTEGER: 26
This article source code download (does not contain library files): http://free.ys168.com /? Javamxj
Springbasic.zip.