Quick Start with spring-10. Replacement of any method

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

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.