码迷,mamicode.com
首页 > 编程语言 > 详细

java 动态代理

时间:2018-05-12 17:27:02      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:eth   Servle   cer   main   system   targe   target   vax   处理   

动态代理

//---------------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 动态代理

标签:eth   Servle   cer   main   system   targe   target   vax   处理   

原文地址:https://www.cnblogs.com/ms-grf/p/9029135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!