Tomcat 中的自定义 MBean - 创建 InitialContext 时找不到 javaURLContextFactory
我编写了一个部署在 Tomcat 6 中的自定义 MBean。它的任务之一是查询数据库值。我通过使用 JNDI 加载数据库资源来完成此操作 - 该资源是在 Tomcat 的 server.xml 中定义的。
问题是,当我创建 javax.naming.InitialContext 实例时,它会抛出 ClassNotFoundException,因为它找不到 org.apache.naming.java。 javaURLContextFactory。 该类位于 catalina.jar
中并由公共类加载器加载。包含我的 MBean 代码的 jar 由共享类加载器加载。
关于如何解决这个问题有什么想法吗?
注意:我的 MBean 是由我在 tomcat/conf/web.xml 中定义的 ContextListener 加载的。我还在 webapp web.xml
中定义了它,这没有什么区别。我无法真正移动我的 jar 以便由公共类加载器加载,因为它依赖于共享类加载器加载的类。
预先致谢,
威尔
I've written a custom MBean that deploys in Tomcat 6. One of its tasks is to query a database value. I'm doing this by loading up the database resource using JNDI - the resource is defined in Tomcat's server.xml.
The problem is that when I create an instance of javax.naming.InitialContext
it throws a ClassNotFoundException
as it can't find org.apache.naming.java.javaURLContextFactory
.
This class is in catalina.jar
and loaded by the common classloader. The jar containing my MBean code is loaded by a shared classloader.
Any ideas as to how I can get around this?
To note: my MBean is loaded by a ContextListener which I've defined in the tomcat/conf/web.xml
. I've also defined it in a webapp web.xml
which makes no difference. I can't really move my jar so as to be loaded by the common classloader as it relies on classes loaded by the shared classloader.
Thanks in advance,
Will
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来是一个奇怪的类加载或安全/权限问题。下面是一个解决方法。
主要思想:由于
ServletContextListener
可以调用new InitialContext()
而无需ClassNotFoundException
在侦听器中获取它并将其传递给构造函数在注册 MBean 之前先获取 MBean 对象。我使用了一个简单的 Web 应用程序,并且没有修改tomcat/conf/web.xml
。tomcat/conf/context.xml
中的资源配置:web.xml
资源配置:注册 MBean 的
ServletContextListener
:MBean 接口:
MBean 实现:
MBean 描述符操作方法 说
mbeans-descriptor.xml
是必需的,但没有它也能正常工作。我可以使用jconsole
连接到 HelloMBean。通过jconsole
调用sayHello()
打印以下内容:Sources:
It looks a weird classloading or security/permission issue. Below is a workaround.
The main idea: Since the
ServletContextListener
could call thenew InitialContext()
without theClassNotFoundException
get it in the listener and pass it to the constructor of the MBean object before you register the MBean. I used a simple web application and I have not modifiedtomcat/conf/web.xml
.Resource configuration in the
tomcat/conf/context.xml
:The
web.xml
resource configuration:The
ServletContextListener
which registers the MBean:MBean interface:
MBean implementation:
The MBean Descriptor How To says a
mbeans-descriptor.xml
is required but it's worked without it well. I could connect to the HelloMBean withjconsole
. CallingsayHello()
throughjconsole
printed the following:Sources: