java 动态代理

Posted 发福大叔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 动态代理相关的知识,希望对你有一定的参考价值。

动态代理

//---------------TargetInterface
package cn.ms.proxy;

public interface TargetInterface {

    public void method1();
    public String method2();
    public int method3(int x);
    
}

//---------------Target
package cn.ms.proxy;

public class Target implements TargetInterface {

    @Override
    public void method1() {
        System.out.println("method1 running...");
    }

    @Override
    public String method2() {
        System.out.println("method2 running...");
        return "method2";
    }

    @Override
    public int method3(int x) {
        System.out.println("method3 running...");
        return x;
    }

}

//---------------ProxyTest
package cn.ms.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {
        Target target = new Target();
        TargetInterface newProxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(), 
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("目标方法前逻辑");
                        Object invoke = method.invoke(target, args);
                        System.out.println("目标方法后逻辑");
                        return invoke;
                    }
                });
        newProxy.method1();
        System.out.println("------------------ method1  end ------------------");
        String returnMethod2 = newProxy.method2();
        System.out.println(returnMethod2);
        System.out.println("------------------ method2  end ------------------");
        int returnMethod3 = newProxy.method3(7);
        System.out.println(returnMethod3);
        System.out.println("------------------ method3  end ------------------");
    }
    
}

动态代理使用(处理全局乱码的问题)

package com.ithiema.web.filter;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class EncodingProxyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
            final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletRequest enhanceRequest = (HttpServletRequest) Proxy.newProxyInstance(
                    httpServletRequest.getClass().getClassLoader(),
                    httpServletRequest.getClass().getInterfaces(),
                    new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            if(method.getName().equals("getParameter")){
                                String invoke = (String) method.invoke(httpServletRequest, args);
                                invoke = new String(invoke.getBytes("iso-8859-1"), "UTF-8");
                                return invoke;
                            }
                            return method.invoke(httpServletRequest, args);
                        }
                    });
            chain.doFilter(enhanceRequest, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    
    @Override
    public void destroy() {

    }

}

 

以上是关于java 动态代理的主要内容,如果未能解决你的问题,请参考以下文章

Java设计模式-代理模式之动态代理(附源代码分析)

(java反射-JDK动态代理)+CGLIB动态代理

Java动态代理

Java动态代理

Java动态代理

动态代理