一 。先定义一个接口
[color=orange]public interface User {
public void addUser(String username, String password);
public void deleteUser(int id);
}[/color]
二。实现User接口
[color=orange]public class UserImpl implements User {
public void addUser(String username, String password) {
System.out.println("-----------addUser----------");
}
public void deleteUser(int id) {
System.out.println("-----------deleteUser----------");
}
}[/color]
三。有业务需求时,我们要进行改造,比方说要增加安全检测对每个addUser和deleteUser方法,为了不使原代码的破坏性,我们要新增加代理来进行处理这个安全检测(新增加一个类远比修改原类代码简单的多),只是一个代理,并不破坏原类,代理我们可以分为动态代理和静态代理,静态代理有失一些扩展性,在此,我们采用动态代理,步骤如下:
1.实现InvocationHandler接口
2.实现invoke方法
[color=orange]public class SecurityCheck implements InvocationHandler {
private Object ob;
public Object newProxy(Object ob){
this.ob = ob;
return Proxy.newProxyInstance(ob.getClass().getClassLoader(), ob.getClass().getInterfaces(), this);
}
public Object invoke(Object a, Method method, Object[] args)
throws Throwable {
try{
checkSecurity();
Object ret = (Object)method.invoke(ob, args);
return ret;
}catch(Exception e ){
e.printStackTrace();
}
return null;
}
private void checkSecurity(){
System.out.println("-----------Security check------------");
}
}[/color]
四。测试类
[color=orange] /**
* @param args
*/
public static void main(String[] args) {
//UserImpl userImpl = new UserImpl();
SecurityCheck proxy= new SecurityCheck();
User user = (User)proxy.newProxy(new UserImpl());
user.addUser("", "");
}[/color]
控制台打印出
[color=orange]-----------Security check------------
-----------addUser----------[/color]
[color=orange]public interface User {
public void addUser(String username, String password);
public void deleteUser(int id);
}[/color]
二。实现User接口
[color=orange]public class UserImpl implements User {
public void addUser(String username, String password) {
System.out.println("-----------addUser----------");
}
public void deleteUser(int id) {
System.out.println("-----------deleteUser----------");
}
}[/color]
三。有业务需求时,我们要进行改造,比方说要增加安全检测对每个addUser和deleteUser方法,为了不使原代码的破坏性,我们要新增加代理来进行处理这个安全检测(新增加一个类远比修改原类代码简单的多),只是一个代理,并不破坏原类,代理我们可以分为动态代理和静态代理,静态代理有失一些扩展性,在此,我们采用动态代理,步骤如下:
1.实现InvocationHandler接口
2.实现invoke方法
[color=orange]public class SecurityCheck implements InvocationHandler {
private Object ob;
public Object newProxy(Object ob){
this.ob = ob;
return Proxy.newProxyInstance(ob.getClass().getClassLoader(), ob.getClass().getInterfaces(), this);
}
public Object invoke(Object a, Method method, Object[] args)
throws Throwable {
try{
checkSecurity();
Object ret = (Object)method.invoke(ob, args);
return ret;
}catch(Exception e ){
e.printStackTrace();
}
return null;
}
private void checkSecurity(){
System.out.println("-----------Security check------------");
}
}[/color]
四。测试类
[color=orange] /**
* @param args
*/
public static void main(String[] args) {
//UserImpl userImpl = new UserImpl();
SecurityCheck proxy= new SecurityCheck();
User user = (User)proxy.newProxy(new UserImpl());
user.addUser("", "");
}[/color]
控制台打印出
[color=orange]-----------Security check------------
-----------addUser----------[/color]