[英]Creating a custom JNDI resource
我想通过context / JNDI为我正在开发的web-app提供配置。 我目前正在使用捆绑的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: No object bound to name 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.