package com. wyl. demo02 ;
public interface UserServlet {
public void select ( ) ;
public void delete ( ) ;
public void update ( ) ;
public void insert ( ) ;
}
package com. wyl. demo02 ;
public class UserServletImpl implements UserServlet {
@Override
public void select ( ) {
System . out. println ( "查询了一个用户" ) ;
}
@Override
public void delete ( ) {
System . out. println ( "删除了一个用户" ) ;
}
@Override
public void update ( ) {
System . out. println ( "修改了一个用户" ) ;
}
@Override
public void insert ( ) {
System . out. println ( "添加了一个用户" ) ;
}
}
package com. wyl. demo04 ;
import com. wyl. demo03. Rent ;
import java. lang. reflect. InvocationHandler ;
import java. lang. reflect. Method ;
import java. lang. reflect. Proxy ;
public class ProxyInvocationHandler implements InvocationHandler {
private Object object;
public void setObject ( Object object) {
this . object = object;
}
public Object getProxy ( ) {
return Proxy . newProxyInstance ( this . getClass ( ) . getClassLoader ( ) ,
object. getClass ( ) . getInterfaces ( ) , this ) ;
}
@Override
public Object invoke ( Object o, Method method, Object [ ] objects) throws Throwable {
log ( method. getName ( ) ) ;
Object invoke = method. invoke ( object, objects) ;
return invoke;
}
public void log ( String msg) {
System . out. println ( "执行了" + msg+ "方法" ) ;
}
}
package com. wyl. demo04 ;
import com. wyl. demo02. UserServlet ;
import com. wyl. demo02. UserServletImpl ;
import com. wyl. demo03. Rent ;
public class Client {
public static void main ( String [ ] args) {
UserServletImpl userServlet = new UserServletImpl ( ) ;
ProxyInvocationHandler prh = new ProxyInvocationHandler ( ) ;
prh. setObject ( userServlet) ;
UserServlet proxy = ( UserServlet ) prh. getProxy ( ) ;
proxy. delete ( ) ;
}
}