java 動態代理

來源:互聯網
上載者:User

標籤:eth   Servle   cer   main   system   targe   target   vax   處理   

動態代理
//---------------TargetInterfacepackage cn.ms.proxy;public interface TargetInterface {    public void method1();    public String method2();    public int method3(int x);    }//---------------Targetpackage 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;    }}//---------------ProxyTestpackage 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 動態代理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.