【问题标题】:Tomcat JNDI returns null instead of beanTomcat JNDI 返回 null 而不是 bean
【发布时间】:2012-12-25 12:52:51
【问题描述】:

我正在尝试使用 GlobalNamingResources 创建一个将由 2 个不同的 webapps 使用的全局 bean(当然在同一个 tomcat 上)。

我的问题是,当我尝试将新数据设置到从 JNDI 获得的类时,我得到了 NullPointerException。

我点击了以下链接,但我仍然不确定我做错了什么: http://tomcat.apache.org/tomcat-7.0-doc/config/globalresources.html

这是使我的 servlet 崩溃的行:

single.setText("TEXT");

这是我的测试 servlet:

     @WebServlet("/JNDIServlet")
     public class JNDIServlet extends HttpServlet {

     /**
      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      */
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {

              Thread.sleep(4000);
              Context initCtx = new InitialContext();
              Context envCtx = (Context) initCtx.lookup("java:comp/env");
              SingletonClass single = (SingletonClass) envCtx.lookup("TestMe");
              single.setText("TEXT");
              initCtx.rebind("TestMe", single);
              response.getWriter().println(envCtx.lookup("TestMe").toString());
         } catch (NamingException e) {
              e.printStackTrace();
         } catch (InterruptedException e) {
        e.printStackTrace();
         }
     }

我的“SingletonClass”只是一个pojo:

package com.jndi.test;

public class SingletonClass {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public SingletonClass() {
    }

}

这是我的 web.xml 文件:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>JNDIServletTest</display-name>

  <resource-ref>
<res-ref-name>TestMe</res-ref-name>
<res-type>com.jndi.test.SingletonClass</res-type>
<res-auth>Container</res-auth>
  </resource-ref>
</web-app>

最后,来自 tomcat 的 server.xml 文件中的相关部分:

<GlobalNamingResources>

    <Resource name="TestMe" auth="Container"
                    type="com.jndi.test.SingletonClass"
                    factory="org.apache.naming.factory.BeanFactory"/>

<!-- Editable user database that can also be used by
     UserDatabaseRealm to authenticate users
-->
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>

另外,我在我的 tomcat 的 context.xml 中添加了所需的资源链接:

<ResourceLink name="TestMe" global="TestMe" type="com.jndi.test.SingletonClass"/>

谁能告诉我我做错了什么?

非常感谢:)

【问题讨论】:

  • 下定决心。是NullPointerException,根据您的问题,还是ClassCastException,根据您对现已删除的答案的评论?

标签: java tomcat nullpointerexception jndi


【解决方案1】:

[已解决]

第 1 部分:
在 webapp 的 context.xmlResourceLink 中,使用与 global 中使用的 name 不同的 name nameResource 中为 Resource 定义的server.xml

第 2 部分:
删除资源 bean from-webapp's-lib 的重复实现 JAR/类,并保留放在 tomcat lib 中的实现。

第 2 部分解释:
就我而言,我在一个单独的包中创建了一个工厂并将其打包为 JAR。我在 webapp 的 lib 和 tomcat lib 中都使用了 JAR。
这导致了 ClassCastException(因为资源是使用 tomcat 的版本类创建的 - 然后应用程序试图将其从它自己的 lib 中转换为该类[如果有兴趣,请阅读更多关于 tomcat 中的 ClassLoaders 的内容,这将使事情变得更加清晰])。

参考:JNDI ClassCastException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2018-11-28
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多