java - 创建自定义 JNDI 资源

标签 java netbeans glassfish jndi netbeans-8

我想通过 context/JNDI 为我正在开发的 Web 应用程序提供配置。我目前正在使用捆绑的 Glassfish 服务器在 Netbeans 8.1 中进行开发,尽管我的解决方案应该与容器无关。

我有用于获取数据库连接的工作设置,但对自定义资源类型感到困惑。

在 web.xml 中:

<resource-ref>
    <res-ref-name>SHOWmail/search</res-ref-name>
    <res-type>com.example.SearchProvider</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

在 glassfish-resources.xml 中:

    <custom-resource jndi-name="SHOWmail/search" res-type="com.example.SearchProvider" factory-class="com.example.SearchProviderFactory">
    <property name="name" value="value"/>
</custom-resource>

在代码中:

   initContext = new InitialContext();
   envContext = (Context) initContext.lookup("java:comp/env");
   search = (SearchProvider)envContext.lookup("SHOWmail/search");

我可靠地得到javax.naming.NameNotFoundException:没有对象绑定(bind)到名称java:SHOWmail/search。我的工厂和类没有被触及(如果需要的话会添加)。

非常感谢指出我出错的地方。

最佳答案

看来我误解了 netbeans/glassfish 组合中不同的 JNDI 命名空间。

解决方案是查看java:app而不是java:comp/env;这将搜索 WEB-INF/glassfish-resources.xml。

web.xml

<resource-env-ref>
    <resource-env-ref-name>SHOWmail/search</resource-env-ref-name>
    <resource-env-ref-type>com.example.SearchProviderFactory</resource-env-ref-type>
</resource-env-ref>

glassfish-resources.xml

<custom-resource jndi-name="java:app/SHOWmail/search" res-type="com.example.ElasticSearchProvider" factory-class="com.example.SearchProviderFactory">
</custom-resource>

关于java - 创建自定义 JNDI 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982996/

相关文章:

java - GlassFish Server,部署,空,假

java - 配置本地主机 https glassfish

Java:如何将默认值设置为另一个注释作为其属性的注释?

NetBeans ("6.8"和 "later") - UML 支持吗?

java - JinternalFrame 未显示,也没有任何异常

java - Netbeans Android 模拟器未运行程序

url-rewriting - 使用 Quercus 的 PHP 重写 Glassfish 上的 URL

java - 改变变量会导致改变对象

Java - 在同一步骤中获得商和余数?

java - for 方法中的索引不增加