简体   繁体   English

Tomcat JNDI返回null而不是bean

[英]Tomcat JNDI returns null instead of bean

I am trying to use GlobalNamingResources to create a global bean that will be used by 2 different webapps (on the same tomcat of course). 我正在尝试使用GlobalNamingResources创建一个将由2个不同的webapp(当然是在同一tomcat上)使用的全局bean。

My problem is that I get a NullPointerException when I try to set new data to the class I got from the JNDI. 我的问题是,当我尝试将新数据设置为从JNDI获得的类时,出现NullPointerException。

I followed the following link, and I am still not sure what I did wrong: http://tomcat.apache.org/tomcat-7.0-doc/config/globalresources.html 我点击了以下链接,但仍然不确定我做错了什么: http : //tomcat.apache.org/tomcat-7.0-doc/config/globalresources.html

This is the line that crashes my servlet: 这是使我的servlet崩溃的行:

single.setText("TEXT");

this is my test servlet: 这是我的测试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();
         }
     }

my "SingletonClass" is just a pojo: 我的“ SingletonClass”只是一个波霍:

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() {
    }

}

this is my web.xml file: 这是我的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>

and finally, the relevant part from my server.xml file from the tomcat: 最后是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>

also, I have added the needed Resource link in the context.xml in my tomcat: 另外,我在tomcat的context.xml中添加了所需的Resource链接:

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

Can anyone please provide me some insight about what I did wrong? 谁能为我提供一些有关我做错了什么的见解?

Thanks a lot :) 非常感谢 :)

[SOLVED] [解决了]

Part 1: 第1部分:
In ResourceLink of webapp's context.xml , use a different name from that used for global name defined for Resource in server.xml 在webapp的context.xml ResourceLink中,使用与在server.xmlResource定义的global name不同的name

Part 2: 第2部分:
Remove duplicate implementation JARs/classes of the resource bean from-webapp's-lib and keep the ones placed in tomcat lib. 从-webapp-s-lib中删除资源bean的重复实现JAR /类 ,并将其放置在tomcat lib中。

Part 2 Explained: 第2部分说明:
In my case, i had created a factory in a separate package and packaged it as a JAR. 就我而言,我是在一个单独的程序包中创建了一个工厂,并将其打包为一个JAR。 I was using the JAR in both webapp's lib and tomcat lib. 我在webapp的lib和tomcat的lib中都使用了JAR。
This resulted in a ClassCastException (caused as the Resource was created with tomcat's version class - and then the application tried to cast it to the class from it's own lib [if interested, read a little more about ClassLoaders in tomcat, which will make things a lot clearer]). 这导致ClassCastException (由于Resource是使用tomcat的版本类创建的-然后应用程序尝试将其从自己的lib强制转换为该类[如果感兴趣,请阅读tomcat中有关ClassLoaders的更多信息,这将使清晰得多])。

Reference: JNDI ClassCastException 参考: JNDI ClassCastException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM