2016-05-02 60 views
1

我想爲通過上下文/ JNDI開發的Web應用程序提供配置。我目前使用捆綁的Glassfish服務器在Netbeans 8.1中開發,儘管我的解決方案應該是容器不可知的。創建自定義JNDI資源

我有用於獲取數據庫連接的工作設置,但難以理解自定義資源類型。

在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。我的工廠和班級沒有碰觸(如果需要,將添加)。

指向我出錯的地方非常感謝。

+0

我也試着'<資源-ENV-REF>'代替'<資源引用>',具有相同的結果。 –

回答

0

似乎我誤解了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>