Tomcat ServletContext 无法加载 class
Tomcat ServletContext cannot load class
我需要在两个网络应用程序之间进行通信。两者都是 tomcat 个项目。我想避免使用 http 请求进行通信。在做了一些研究之后,我发现有一个 ServletContext 对象可以处理这个问题。
我正在按照 http://blog.imaginea.com/cross-context-communication-between-web-applications/ 上的指南进行操作,并决定在我自己的快速入门中尝试这个。
我确保我的 tomcat 服务器的 crossContext 设置为 true。
<Context crossContext="true">
我创建了两个 wicket quickstarts,一个名为 bar,一个名为 foo。我的想法是我可以从 bar 调用 foo 中的函数。
这是栏中的代码
String methodName = getRequest().getParameter("PARAM_METHOD");
ServletContext srcServletContext = ((WebApplication)WebApplication.get()).getServletContext();
ServletContext targetServletContext = srcServletContext.getContext("/foo");
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Object object = targetServletContext.getAttribute("org.apache.wicket.protocol.http.WicketServlet.CONTEXT.foo");
ClassLoader targetServiceClassLoader = targetServletContext.getClass().getClassLoader();
Thread.currentThread().setContextClassLoader(targetServiceClassLoader);
// Causes a ClassNotFoundException
Class<?> classBarService = (Class<?>)targetServiceClassLoader.loadClass("com.foo.SomeUtil");
Method getTextMethod = object.getClass().getMethod("getText", String.class);
Object someUtil = getTextMethod.invoke(object, "someUtil");
Method targetMethod = classBarService.getMethod(methodName, (Class[])null);
Object responseFromTextMethod = targetMethod.invoke(someUtil, (Object[])null);
}
catch (Exception e)
{
text += e.toString();
}
finally
{
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
foo 在包 com.foo 中有一个名为 SomeUtil 的 class。
但是,当我在更改 class 加载程序后尝试加载 class 时,我得到了 "ClassNotFoundException: com.foo.SomeUtil"。我真的不知道我做错了什么。
提前感谢任何帮助。
即使将 CrossContext 设置为 true
,调用 class 的类加载器也需要访问它正在尝试加载的 class。
尝试将包含 classes 的 jar 部署到两个 Web 应用程序。
引自教程:
Solution-1: If we could externalize the custom data type classes which are accessed by multiple web applications (here accessed by ‘Foo’ and ‘Bar’) to a different library and place it inside the commons library location of the web container (In case of tomcat, it is <TOMCAT_HOME>\lib),
要么将 jar 部署到两个 Web 应用程序,要么部署到 commons/lib,但据我所知,tomcat 的较新版本默认不再提供 commons/lib
你试过更换
ClassLoader targetServiceClassLoader = targetServletContext.getClass().getClassLoader();
和
ClassLoader targetServiceClassLoader = object.getClass().getClassLoader();
喜欢教程中提出的吗?
我需要在两个网络应用程序之间进行通信。两者都是 tomcat 个项目。我想避免使用 http 请求进行通信。在做了一些研究之后,我发现有一个 ServletContext 对象可以处理这个问题。
我正在按照 http://blog.imaginea.com/cross-context-communication-between-web-applications/ 上的指南进行操作,并决定在我自己的快速入门中尝试这个。
我确保我的 tomcat 服务器的 crossContext 设置为 true。
<Context crossContext="true">
我创建了两个 wicket quickstarts,一个名为 bar,一个名为 foo。我的想法是我可以从 bar 调用 foo 中的函数。
这是栏中的代码
String methodName = getRequest().getParameter("PARAM_METHOD");
ServletContext srcServletContext = ((WebApplication)WebApplication.get()).getServletContext();
ServletContext targetServletContext = srcServletContext.getContext("/foo");
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Object object = targetServletContext.getAttribute("org.apache.wicket.protocol.http.WicketServlet.CONTEXT.foo");
ClassLoader targetServiceClassLoader = targetServletContext.getClass().getClassLoader();
Thread.currentThread().setContextClassLoader(targetServiceClassLoader);
// Causes a ClassNotFoundException
Class<?> classBarService = (Class<?>)targetServiceClassLoader.loadClass("com.foo.SomeUtil");
Method getTextMethod = object.getClass().getMethod("getText", String.class);
Object someUtil = getTextMethod.invoke(object, "someUtil");
Method targetMethod = classBarService.getMethod(methodName, (Class[])null);
Object responseFromTextMethod = targetMethod.invoke(someUtil, (Object[])null);
}
catch (Exception e)
{
text += e.toString();
}
finally
{
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
foo 在包 com.foo 中有一个名为 SomeUtil 的 class。 但是,当我在更改 class 加载程序后尝试加载 class 时,我得到了 "ClassNotFoundException: com.foo.SomeUtil"。我真的不知道我做错了什么。
提前感谢任何帮助。
即使将 CrossContext 设置为 true
,调用 class 的类加载器也需要访问它正在尝试加载的 class。
尝试将包含 classes 的 jar 部署到两个 Web 应用程序。
引自教程:
Solution-1: If we could externalize the custom data type classes which are accessed by multiple web applications (here accessed by ‘Foo’ and ‘Bar’) to a different library and place it inside the commons library location of the web container (In case of tomcat, it is <TOMCAT_HOME>\lib),
要么将 jar 部署到两个 Web 应用程序,要么部署到 commons/lib,但据我所知,tomcat 的较新版本默认不再提供 commons/lib
你试过更换
ClassLoader targetServiceClassLoader = targetServletContext.getClass().getClassLoader();
和
ClassLoader targetServiceClassLoader = object.getClass().getClassLoader();
喜欢教程中提出的吗?