This article describes how to replace any method in section 3.3.3.2 of Spring framework development reference manual ".
Take a closer look at the document. ·
First create a package: javamxj. Spring. Basic. methodreplacer, and then place the following five files under this package.
| Hello. Java |
| Package javamxj. Spring. Basic. methodreplacer; Public interface Hello { Public void sayhello (string S ); } |
| Helloimpl. Java |
| Package javamxj. Spring. Basic. methodreplacer; Public class helloimpl implements Hello { Public void sayhello (string name ){ System. Out. println ("Hello:" + name );
} } |
| Helloreplacer. Java |
| Package javamxj. Spring. Basic. methodreplacer; Import java. Lang. Reflect. method; Import org. springframework. Beans. Factory. Support. methodreplacer; Public class helloreplacer implements methodreplacer { Public object reimplement (Object o, method M, object [] ARGs) throws throwable { System. Out. println ("Hello:" + ARGs [0]); Return NULL;
} }
|
| 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 = "helloreplacer" Class = "javamxj. Spring. Basic. methodreplacer. helloreplacer"/>
<Bean id = "helloa" Class = "javamxj. Spring. Basic. methodreplacer. helloimpl"/>
<Bean id = "hellob" Class = "javamxj. Spring. Basic. methodreplacer. helloimpl"> <Replaced-method name = "sayhello" replacer = "helloreplacer"/>
</Bean>
</Beans> |
| Main. Java |
| Package javamxj. Spring. Basic. methodreplacer; 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/methodreplacer/beans. xml ");
Beanfactory Ft = new Xmlbeanfactory (RES ); // Replaced-method not used Hello H = (Hello) ft. getbean ("helloa "); H. sayhello ("Sharing Java ");
// Use replaced-Method H = (Hello) Ft. getbean ("hellob ");
H. sayhello ("Sharing Java "); } }
|
Brief description:
· Hello is an interface class that implements interface-oriented programming.
·
The helloimpl class implements the hello interface and outputs a simple statement.
·
The helloreplacer class must implement the methodreplacer interface, and the replacement method is provided in the reimplement.
·
Beans. xml defines three beans. helloreplacer points to the helloreplacer class. helloa and hellob both point to the helloimpl class, where hellob defines the replaced-method.
·
In the main class, compare the statements before and after using replaced-method.
· This time we need
The cglib-nodep-2.1_2.jar in the spring-framework home directory/lib/cglib directory is added to
In libraries, use the dynamic proxy.
Running result:Hello: Happy Java
Hello: Share Java happy this article source code download (does not contain library files): http://free.ys168.com /? Javamxj
Springbasic.zip.