java给类添加默认值,如何将我的自定义类加载器设置为默认值?

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?

Thanks!

解决方案

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

java -Djava.system.class.loader

=com.test.YourCustomClassLoader com.test.YourMainClass

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.

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 Loader Component: How to define your custom Loader element for your own Context.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值