1.proxy代理模式
代理模式为其他对象提供一种代理,并以控制对这个对象的访问。通过代理可以实现对象的延迟加载和对象的访问控制,在具体调用的前后执行特殊处理或者是完全改变具体实现。代理模式能够协调调用者和被调用者,在一定程度上降低了系统的耦合度。
代理模式结构图如下:
2.java动态代理
jdk通过InvocationHandler和Proxy来实现对接口的动态代理,cglib用于对具体类实现动态代理,其内部是通过创建具体类的子类来实现动态代理的,所以不支持对final类及final方法动态代理,对final类动态代理会报:java.lang.IllegalArgumentException: Cannot subclass final class class javaProxy.HelloWordImpl;对非final类的动态代理,其final方法则不会执行动态代理,直接调用具体类的final方法,其他方法则执行动态代理。
具体代码示例:
//接口定义
public interface HelloWord {
public void sayHelloWord();
public void sayHelloWord(String name);
public String getHelloWord();
public String getHelloWord(String name);
}
//接口具体实现
public class HelloWordImpl implements HelloWord {
@Override
public void sayHelloWord() {
System.out.println("HelloWord");
}
@Override
public void sayHelloWord(String name) {
System.out.println("HelloWord " + name);
}
@Override
public final String getHelloWord() {
return "HelloWord";
}
@Override
public String getHelloWord(String name) {
return "HelloWord " + name;
}
}
//jdk动态代理
public class HelloWordInvocationHandler implements InvocationHandler {
Object targe;
public HelloWordInvocationHandler(Object targe){
this.targe = targe;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
doBefore(method, args);
Object result = method.invoke(targe, args);
doAfter(method, args, result);
return result;
}
private void doBefore(Method method, Object[] args){
StringBuilder sb = new StringBuilder("javaProxy doBefore: ");
sb.append(method.getName()).append('(');
if(args != null){
for(Object arg : args)
sb.append(arg).append(',');
}
if(sb.charAt(sb.length()-1) == ',')
sb.deleteCharAt(sb.length()-1);
sb.append(')');
System.out.println(sb.toString());
}
private void doAfter(Method method, Object[] args, Object result){
StringBuilder sb = new StringBuilder("javaProxy doAfter: ");
sb.append(method.getName()).append('(');
if(args != null){
for(Object arg : args)
sb.append(arg).append(',');
}
if(sb.charAt(sb.length()-1) == ',')
sb.deleteCharAt(sb.length()-1);
sb.append(')').append(" result:").append(result);
System.out.println(sb.toString());
}
}
//cglib动态代理
public class HelloWordCglibProxy implements MethodInterceptor{
public Object getProxyInstance(Class targetClass) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(targetClass);
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
doBefore(method, args);
Object result = proxy.invokeSuper(target, args);
doAfter(method, args, result);
return result;
}
private void doBefore(Method method, Object[] args){
StringBuilder sb = new StringBuilder("cglibProxy doBefore: ");
sb.append(method.getName()).append('(');
if(args != null){
for(Object arg : args)
sb.append(arg).append(',');
}
if(sb.charAt(sb.length()-1) == ',')
sb.deleteCharAt(sb.length()-1);
sb.append(')');
System.out.println(sb.toString());
}
private void doAfter(Method method, Object[] args, Object result){
StringBuilder sb = new StringBuilder("cglibProxy doAfter: ");
sb.append(method.getName()).append('(');
if(args != null){
for(Object arg : args)
sb.append(arg).append(',');
}
if(sb.charAt(sb.length()-1) == ',')
sb.deleteCharAt(sb.length()-1);
sb.append(')').append(" result:").append(result);
System.out.println(sb.toString());
}
}
//测试类
public class HelloWordTest {
public static void main(String... args) throws Exception{
System.out.println("--------------java Proxy--------------");
HelloWordInvocationHandler handler = new HelloWordInvocationHandler(new HelloWordImpl());
Class proxyClass = Proxy.getProxyClass(
HelloWord.class.getClassLoader(), new Class[] { HelloWord.class });
HelloWord helloWord1 = (HelloWord) proxyClass.
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });
helloWord1.sayHelloWord();
helloWord1.sayHelloWord("beautiful girl");
HelloWord helloWord2 = (HelloWord) Proxy.newProxyInstance(HelloWord.class.getClassLoader(),
new Class[] { HelloWord.class },
handler);
helloWord2.getHelloWord();
helloWord2.getHelloWord("beautiful girl");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("--------------cglib Proxy--------------");
HelloWordCglibProxy cglibProxy = new HelloWordCglibProxy();
HelloWordImpl helloWordImpl = (HelloWordImpl)cglibProxy.getProxyInstance(HelloWordImpl.class);
helloWordImpl.sayHelloWord();
helloWordImpl.sayHelloWord("beautiful girl");
helloWordImpl.getHelloWord();
helloWordImpl.getHelloWord("beautiful girl");
}
}
输入如下:
--------------java Proxy--------------
javaProxy doBefore: sayHelloWord()
HelloWord
javaProxy doAfter: sayHelloWord() result:null
javaProxy doBefore: sayHelloWord(beautiful girl)
HelloWord beautiful girl
javaProxy doAfter: sayHelloWord(beautiful girl) result:null
javaProxy doBefore: getHelloWord()
javaProxy doAfter: getHelloWord() result:HelloWord
javaProxy doBefore: getHelloWord(beautiful girl)
javaProxy doAfter: getHelloWord(beautiful girl) result:HelloWord beautiful girl
--------------cglib Proxy--------------
cglibProxy doBefore: sayHelloWord()
HelloWord
cglibProxy doAfter: sayHelloWord() result:null
cglibProxy doBefore: sayHelloWord(beautiful girl)
HelloWord beautiful girl
cglibProxy doAfter: sayHelloWord(beautiful girl) result:null
cglibProxy doBefore: getHelloWord(beautiful girl)
cglibProxy doAfter: getHelloWord(beautiful girl) result:HelloWord beautiful girl