Giriş
Açıklaması şöyle. JNDI isimleri container 'a mahsus olabilir. Container değişince uygulamamız etkilenmesin istiyorsak web.xml içinde <resource-ref> kullanılır
You can always refer to resources in your application directly by their JNDI name as configured in the container, but if you do so, essentially you are wiring the container-specific name into your code. This has some disadvantages, for example, if you'll ever want to change the name later for some reason, you'll need to update all the references in all your applications, and then rebuild and redeploy them.<resource-ref> introduces another layer of indirection: you specify the name you want to use in the web.xml, and, depending on the container, provide a binding in a container-specific configuration file.
Örnek
Şöyle yaparız.
<resource-ref>
<res-ref-name>jdbc/primaryDB</res-ref-name>
<jndi-name>jdbc/PrimaryDBInTheContainer</jndi-name>
</resource-ref>
Kod içinde bu nesneye "java:comp/env/jdbc/primaryDB" olarak erişiriz. Şöyle yaparız
DataSource ds = (DataSource)envContext.lookup("java:comp/env/jdbc/primaryDB
");
Hiç yorum yok:
Yorum Gönder