如何将我的自定义类加载器设置为默认值? [英] How to set my custom class loader to be the default?

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

问题描述

我正在尝试使用自定义类加载器来练习自己,我还有一些问题。有没有办法指示JVM全局使用我的自定义类加载器?例如,我编写了在Tomcat 6下运行的小应用程序.servlet由容器管理,我应该在哪里设置我的类加载器?另外,webapp使用了一些第三方罐子,我可以控制这些罐子的类加载吗?

I'm trying to practice myself with custom class loaders, and I've some questions. Is there a way to indicate the JVM to use my custom class loader globally? For example, I wrote small app running under Tomcat 6. The servlet is managed by the container, where should I set my class loader? In addition, the webapp uses some 3rd party jars, can I control the classes loading of those jars?

如果是独立应用程序,上面的答案会有所不同吗? ?

Are the answers to the above will be different in case of standalone app?

谢谢!

推荐答案

您可以设置系统默认类loader作为JVM参数:

You can set the system default class loader as a JVM argument:

java -Djava.system.class.loader
    =com.test.YourCustomClassLoader com.test.YourMainClass

当Tomcat作为java应用程序启动时,您也可以设置此参数。 %TOMCAT_HOME%\ bin\run.bat $ TOMCAT_HOME / bin / run.sh 可执行文件。

As Tomcat starts as a java application, you can set this parameter too, at the %TOMCAT_HOME%\bin\run.bat or $TOMCAT_HOME/bin/run.sh executable.

编辑完成:
如果将类加载器设置为de System类加载器,它将用于加载Tomcat组件类,不同的库和您自己的类。
如果希望类加载器仅用于应用程序类(包括库等),则应为上下文配置 Loader 元素。
Loader元素必须在 Context 元素中定义,可以在 context.xml中定义文件。
更多信息:

Edit for completion: If you set your classloader as de System class loader, it will be used to load Tomcat component classes, the different libraries, and your own classes. If you want your class loader to be used only for your application classes (including libraries and so), you should configure a Loader element for your context. The Loader element must be defined inside your Context element, wich can be defined at a context.xml file. More information:

  • Apache Tomcat: Class Loader HOW-TO: defines how the ClassLoaders work in Tomcat.
  • Tomcat Configuration Reference: The Context Container: how to define your Context element
  • Tomcat Configuration Reference: The Loader Component: How to define your custom Loader element for your own Context.

这篇关于如何将我的自定义类加载器设置为默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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