Use the proxy technology in JDK to implement the AOP Function

Source: Internet
Author: User

Package cn.com. Xinli. Service;

Public interface userdao {
Public void save (string name );

Public void Update (string name, integer personid );

Public String getpersonname (integer personid );
}

 

-----------------------------------------------

Package cn.com. Xinli. Service. impl;
Import org. Apache. log4j. Logger;
Import cn.com. Xinli. Service. userdao;

Public class userdaoimpl implements userdao {

Logger log = logger. getlogger (userdaoimpl. Class );
String username;

Public userdaoimpl (){
}
Public userdaoimpl (string username ){
Super ();
This. Username = username;
}
Public String GetUserName (){
Return username;
}
Public String getpersonname (integer personid ){
System. Out. println ("The getpersonname method is executed ");
Return username;
}
Public void save (string name ){
System. Out. println ("the Save method is executed ");
}
Public void Update (string name, integer personid ){
System. Out. println ("update method executed ");

}

}
--------------------------------------------------------------

Package cn.com. Xinli. AOP;

Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. method;
Import java. Lang. Reflect. proxy;

Import cn.com. Xinli. Service. userdao;
Import cn.com. Xinli. Service. impl. userdaoimpl;

Public class jdkproxyfactory implements invocationhandler {

Private object targetobject;

Public object invoke (Object proxy, method, object [] ARGs)
Throws throwable {
Userdao bean = (userdao) targetobject;
Object result = NULL;
System. Out. println (method. getname () + "start to execute ...");
Result = method. Invoke (targetobject, argS );
System. Out. println (method. getname () + "log end ...");
Return result;
}

Public object createproxyinstance (Object targetobject ){
This.tar GetObject = targetobject;
Return proxy.newproxyinstance(this.tar GetObject. getclass (). getclassloader (),
This.tar GetObject. getclass (). getinterfaces (), this );
}

}

 

-------------------------

Test:

@ Test
Public void test1 (){
Jdkproxyfactory factory = new jdkproxyfactory ();
Userdaoimpl userdao = new userdaoimpl ();
Userdao proxyuserdao = (userdao) Factory. createproxyinstance (userdao );
Proxyuserdao. Save ("Zhang Qi ");
Proxyuserdao. Update ("ca", 1 );
}

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.