开发者社区> 问答> 正文

求教自定义ClassLoader在 Web下的问题

尝试通过自定义ClassLoader 配和 ASM实现AOP.

package com.hkliang.core.web;
 
public class WebClassLoader extends ClassLoader{
     
    public WebClassLoader(){
        super(WebClassLoader.class.getClassLoader());
    }
     
    public Class<?> defineClass(String name, byte[] code) throws ClassNotFoundException{
        return defineClass(name, code, 0, code.length );
    }
}

在Servlet中

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         
    WebClassLoader webClassLoader = new WebClassLoader();
         
    String className = "com.hkliang.core.aop.asm.Test";
    try {       
        Class test = webClassLoader.defineClass(className, PrintClass.getBytesFromBasePath(className));
        ClassLoader cl  =  test.getClassLoader();
        System.out.println( "Test is loader by "   +  cl);
        while  (cl  !=   null ) {
            cl  =  cl.getParent();
            System.out.println(cl);
        }
             
        Test t = (Test)test.newInstance();
             
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

异常信息为
INFO: Server startup in 8967 ms
test is com.hkliang.core.web.WebClassLoader@5eaecb
WebappClassLoader
context:
delegate: false
repository: /WEB-INF/classes/
----------> Parent Classloader:
java.net.URLClassLoader@438d57

java.net.URLClassLoader@438d57
sun.misc.Launcher$AppClassLoader@167198e
sun.misc.Launcher$ExtClassLoader@1beea90
null
8
问题:Servlet中声明的Test与自定义ClassLoader加载的Test不是在同一个ClassLoader中,如何实现自定义ClassLoader通过加载 byte[]类型的字节码,并委托给父加载器进行加载呢,实现AOP的目标呢?

展开
收起
落地花开啦 2016-05-27 15:08:06 2679 0
2 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    楼上的回答是对的。有了这个基础就可以开发AOP、IOC框架了。

    2019-07-17 19:17:57
    赞同 展开评论 打赏
  • 喜欢技术,喜欢努力的人

    自定议的ClassLoader使用反射方式:

    public class WebClassLoader extends ClassLoader {
     
        public WebClassLoader() {
            super(WebClassLoader.class.getClassLoader());
        }
         
        public Class<?> defineClass(String name, byte[] code)
                throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            ClassLoader loader = getParent();
            Class<?> cls = ClassLoader.class;
            Method method = cls.getDeclaredMethod(
                "defineClass", new Class[] { String.class, byte[].class,int.class, int.class });
            method.setAccessible(true);
            Object[] args = new Object[] {name, code, new Integer(0), new Integer(code.length) };
            Class clazz = (Class<?>) method.invoke(loader, args);
            try {
                clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            return clazz;
        }
     
        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            return getParent().loadClass(name);
        }
    }
    2019-07-17 19:17:57
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载