gpt4 book ai didi

java - 创建自定义 JNDI 资源

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:17 25 4
gpt4 key购买 nike

我想通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com