JavaWeb基础系列(十三)类加载器、动态代理

一、类加载器

类加载器就加载字节码文件(.class)
这里写图片描述
类加载器的种类:
BootStrap:引导类加载器:加载都是最基础的文件
ExtClassLoader:扩展类加载器:加载都是基础的文件
AppClassLoader:应用类加载器:三方jar包和自己编写java文件
怎么获得类加载器?(重点)
ClassLoader 字节码对象.getClassLoader();

public class Demo {
    public static void main(String[] args) {
        //获得Demo字节码文件的类加载器
        Class clazz = Demo.class; //获得Demo的字节码对象
        ClassLoader classLoader = clazz.getClassLoader();//获得类加载器
        //getResource的参数路径相对classes(src)
        //获得classes(src)下的任何的资源
        String path = classLoader.getResource("com/long/classloader/jdbc.properties").getPath();
        //classLoader.getResourceAsStream("");
        System.out.println(path);
    }
}

二、动态代理

2.1、什么是代理(中介)

目标对象/被代理对象 —— 房主:真正的租房的方法
代理对象 ——- 黑中介:有租房子的方法(调用房主的租房的方法)
执行代理对象方法的对象 —- 租房的人
流程:我们要租房—–>中介(租房的方法)——>房主(租房的方法)
抽象:调用对象—–>代理对象——>目标对象
动态代理的API:
在jdk的API中存在一个Proxy中存在一个生成动态代理的的方法newProxyInstance:

newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) 

返回值:Object就是代理对象
参数:loader:代表与目标对象相同的类加载器—–目标对象.getClass().getClassLoader()
interfaces:代表与目标对象实现的所有的接口字节码对象数组
h:具体的代理的操作,InvocationHandler接口

栗子1:

目标对象接口:

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

目标对象:

//目标对象
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) {
        return x;
    }
}

代理对象:

//代理对象
public class ProxyTest {
    @Test
    public void test1(){
        //获得动态的代理对象----在运行时 在内存中动态的为Target创建一个虚拟的代理对象
        //objProxy是代理对象 根据参数确定到底是谁的代理对象
        TargetInterface objProxy = (TargetInterface) Proxy.newProxyInstance(
            Target.class.getClassLoader(),      //与目标对象相同的类加载器
            new Class[]{TargetInterface.class},
            new InvocationHandler() {
                    //invoke 代表的是执行代理对象的方法
                    @Override
                    //method:代表目标对象的方法字节码对象
                    //args:代表目标对象的响应的方法的参数
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("目标方法前的逻辑");
                        //执行目标对象的方法
                        Object invoke = method.invoke(new Target(), args);
                        System.out.println("目标方法后的逻辑");
                        return invoke;
                    }
            }
        );
        objProxy.method1();
        String method2 = objProxy.method2();
        //System.out.println(method2);
    }
}

运行结果:
目标方法前的逻辑
method1 running…
目标方法后的逻辑
目标方法前的逻辑
method2 running…
目标方法后的逻辑

栗子2:

package com.proxy;

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

public class ProxyTest2 {
    public static void main(String[] args) {
        final Target target = new Target();
        //动态创建代理对象
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
            target.getClass().getClassLoader(),
            target.getClass().getInterfaces(),
            new InvocationHandler() {
                @Override
                //被执行几次?------- 看代理对象调用方法几次
                //代理对象调用接口相应方法 都是调用invoke
                /*
                 * proxy:是代理对象
                 * method:代表的是目标方法的字节码对象
                 * args:代表是调用目标方法时参数
                 */
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    //反射知识点
                    Object invoke = method.invoke(target, args);//目标对象的相应方法
                    //retrun返回的值给代理对象
                    return invoke;
                }
            }
        );
        //调用invoke---Method:目标对象的method1方法  args:null  返回值null
        proxy.method1();
        //调用invoke---Method:目标对象的method2方法  args:null  返回值method2
        String method2 = proxy.method2();
        ////调用invoke-----Method:目标对象的method3方法 args:Object[]{100}  返回值100
        int method3 = proxy.method3(100);
        System.out.println(method2);
        System.out.println(method3);

    }
}

运行结果:
method1 running…
method2 running…
method2
100

栗子3:全局编码

public class EncodingFilter implements Filter{
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException {
        final HttpServletRequest req = (HttpServletRequest) request;
        //使用动态代理完成全局编码
        HttpServletRequest enhanceRequset = (HttpServletRequest) Proxy.newProxyInstance(
                req.getClass().getClassLoader(),
                req.getClass().getInterfaces(),
                new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            //对getParameter方法进行增强
                            String name = method.getName();//获得目标对象的方法名称
                            if("getParameter".equals(name)){
                                String invoke = (String) method.invoke(req, args);//乱码
                                //转码
                                invoke = new String(invoke.getBytes("iso8859-1"),"UTF-8");
                                return invoke;
                            }
                            return method.invoke(req, args);
                        }
                }
        );
        chain.doFilter(enhanceRequset, response);
    }
    @Override
    public void destroy() {

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

    }
}

转载请标明出处,原文地址:https://blog.csdn.net/weixin_41835916 如果觉得本文对您有帮助,请点击支持一下,您的支持是我写作最大的动力,谢谢。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41835916/article/details/80869293