【问题标题】:simple string value by JNDI in JavaJava中JNDI的简单字符串值
【发布时间】:2015-10-05 08:52:12
【问题描述】:

如何在 tomcat 的配置中设置简单的字符串值,然后在 java 应用程序中读取?

context.xml

<ResourceLink name="global/test" global="testing" type="java.lang.String" />

服务器.xml

<Enviroment name="testing" value="myUser" type="java.lang.String"/>

应用程序中的 web.xml

<resource-env-ref>
    <resource-env-ref-name>global/test</resource-env-ref-name>
    <resource-env-ref-type>java.lang.String</resource-env-ref-type>
</resource-env-ref>

在我的 java 应用程序中

public String getValue(){
    return new JndiDataSourceLookup().getDataSource("global/test").toString();
}

当我运行 tomcat 时,我看到这些错误...

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'global/test'; nested exception is javax.naming.NameNotFoundException: Name [global/test] is not bound in this Context. Unable to find [global].
javax.naming.NameNotFoundException: Name [global/test] is not bound in this Context. Unable to find [global].

【问题讨论】:

    标签: java string jndi


    【解决方案1】:

    在你的 web.xml 中使用,

    <env-entry>
    <description>Sample env entry</description>
    <env-entry-name>isConnected</env-entry-name>
    <env-entry-type>java.lang.Boolean</env-entry-type><!--order matters -->
    <env-entry-value>true</env-entry-value>
    </env-entry>
    

    在代码中,

    try {
     Context initCxt =  new InitialContext();
     Boolean isConn =  (Boolean)initCxt.lookup("java:comp/env/isConnected");
     System.out.println(isConn.toString());
     // one could use relative names into the sub-context
     Context envContext = (Context) initCxt.lookup("java:comp/env");
     Boolean isConn2 = (Boolean)envContext.lookup("isConnected");
     System.out.println(isConn2.toString());
    } catch (NamingException e) {
     e.printStackTrace();
    }
    

    查看这里Naming service tutorial 以更好地了解 InitialContext 和 JNDI。

    【讨论】:

    • 在这一行之后 Boolean isConn = (Boolean)initCxt.lookup("java:comp/env/isConnected");是异常 javax.naming.NoInitialContextException: 需要在环境或系统属性中指定类名,或作为 aplet 参数,或在应用程序资源文件中:java.naming.factory.initial
    • 你用的是哪个版本的tomcat?
    • 你能在哪里修复它?
    • 我刚才在 tomcat 8 上运行了完全相同的代码,它可以毫不费力地工作。
    【解决方案2】:

    我不知道 JndiDataSourceLookup().getDataSource("global/test") 里面是什么,但从它的名字来看,它应该返回一个 DataSoruce 而不是一个字符串。

    如果您的查找是本地的,只需执行

    Context ctx = new InitialContext();
    String s = (String) ctx.lookup("global/test");
    

    或者如果你在 javaee 容器中,

    @Resource(name="global/test")
    String testString;
    

    最后在你的ejb-jar.xml

    <env-entry>
        <description>The name was explicitly set in the annotation so the classname prefix isn't required</description>
        <env-entry-name>global/test</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>StringValue</env-entry-value>
    </env-entry>
    

    参考此链接:http://tomee.apache.org/examples-trunk/injection-of-env-entry/README.html

    context.xmlserver.xmlweb.xml 的配置不起作用。

    【讨论】:

    • 由于您使用的是tomcat,并且有一个Web应用程序,您可以在web.xml中添加&lt;env-entry&gt;内容并删除ejb-jar.xml
    • 我将 添加到 web.xml,但是这一行 String s = (String) ctx.lookup("global/test");再次给出异常 javax.naming.NoInitialContextException: 需要在环境或系统属性中指定类名,或作为 aplet 参数,或在应用程序资源文件中:java.naming.factory.initial
    • javax.naming.NoInitialContextException: 需要在环境或系统属性中指定类名,或作为 aplet 参数,或在应用程序资源文件中:java.naming.factory.initial
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多