Quick Start with spring-9. lookup method Injection

Source: Internet
Author: User
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>

Main. Java

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

Libraries
To 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.