Java,自定义类加载器的问题 [英] Java, problems with custom classloader

查看:83
本文介绍了Java,自定义类加载器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义类加载器以加载我的一些类(不是全部).

i'm writing a custom class loader to load some of my classes (not all).

类加载器非常简单:

public Class loadClass(String className, boolean resolve) throws ClassNotFoundException {
    Class cls=findLoadedClass(className);
    if(cls!=null) {
        return cls;
    }
    // Search first for encrypted classes
    cls=decryptClass(className);
    if(cls==null) {
        // Then try with default system classloader
        cls=super.loadClass(className, resolve);
    }
    return cls;
}

这就是我的使用方式:

// In my Launcher class
public static void main(String[] args) {
    MyClassLoader loader=new MyClassLoader();
    try {
        final Class main=loader.loadClass("com.MyAppMain");
        Method toInvoke=main.getMethod("main", args.getClass());
        toInvoke.invoke(main, new Object[]{args});
    }
    catch(Exception ex) {
    }
}

在我的小型测试项目中,一切似乎都很好,但是当我在大型项目(使用spring + hibernate和IoC的客户端-服务器应用程序)中使用此加载程序时,则无法正常工作.我的类加载器中没有特别的异常,但是例如,新的Socket实例在没有真正原因的情况下抛出了"java.net.ConnectException:连接被拒绝" ...

All seem to be fine in my small test project, but when i use this loader in my big project(client-server application that use spring+hibernate and IoC) doesn't work. I have not a particolar exception in my classloader, but for example, new Socket instance throw a "java.net.ConnectException: Connection refused" without a real reason...

其他问题是我的主要形式不可见...和其他类似的奇怪问题.

Other problems is my main form does not become visible... and other strange problems like this.

问题是,这些问题是由我的类加载器导致的,这些类加载器以不同的方式加载了不同种类的类吗?

So, the question, are these problems caused by my classloader that load in different way a different kind of classes?

编辑1

我的项目使用spring,所以我有时使用@Autowired

My project use spring, so i use @Autowired or sometimes

springApplicationContext.getBean(clazz);

注入豆子.

问题是,如果这些类被加密,spring将无法找到我的bean(因此它们需要由我的类加载器加载).有解决此错误的方法吗?谢谢.

The problem is spring cannot find my beans if these classes are encrypted(so they need to be loaded by my classloader). There is a workaround for this mistake? Thanks.

编辑2

我已经在spring ClassPathXmlApplicationContext中设置了我的类加载器,现在我注意到spring使用我的类加载器加载bean类,但是尽管如此,它仍抛出org.springframework.beans.factory.NoSuchBeanDefinitionException,因为它找不到beans ...可以我做?谢谢

I have set my classloader in spring ClassPathXmlApplicationContext and now i notice that spring uses my classloader to load beans class, but despite this it throws an org.springframework.beans.factory.NoSuchBeanDefinitionException becouse it cannot find beans... what can i do? Thanks

推荐答案

我不太擅长类加载器,但是根据您的代码,我们只能假设,如果您的类加载器找不到类,它将重定向到系统类加载器.当您像示例中那样独立运行应用程序时,它可能会正常工作,但是,如果它是在应用程序服务器上运行的Web应用程序,则它将失败.

I'm not very good at class loaders, but from you code it's only possible to assume, that in case your class loader can't find a class, it will redirect to system class loader. It may work fine when you run application standalone, like in your sample, but if it's a web application that is run on application server it will fail.

应用程序服务器通常会创建大型的类加载器层次结构,并具有用于加载应用程序类的单独的类加载器.在这种情况下,系统类加载器对您的Spring相关类一无所知,因此无法加载它们.

Application servers usually create a big hierarchy of class loaders and has a separate class loader used to load your application classes. In this case, system class loader knows nothing about your Spring related classes and thus can't load them.

您必须记住,同一个类可能会由多个类加载器加载,并且如果您尝试比较不同类加载器中的同一个类,则会失败.

You must keep in mind, that same class may be loaded by several class loaders and if you try to compare same class from different class loaders it will fail.

在您的情况下,我将在MyClassLoader构造函数中设置父类加载器.作为父类加载器,您可以使用MyClassLoader.class.getClassLoader().

In your case I would set parent class loader in MyClassLoader constructor. As a parent class loader you can use MyClassLoader.class.getClassLoader() I think.

public class MyClassLoader extends ClassLoader
{
    public MyClassLoader()
    {
       super(MyClassLoader.class.getClassLoader());
    }

    // other code
}

希望这可能会有所帮助:)

Hope this may help :)

这篇关于Java,自定义类加载器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆