Java web.xml 中的资源引用是做什么用的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2887967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:05:11  来源:igfitidea点击:

What is resource-ref in web.xml used for?

javaweb.xmlresource-ref

提问by JMM

I'm just wondering when/why you would define a <resource-ref>element in your web.xmlfile?

我只是想知道何时/为什么要<resource-ref>web.xml文件中定义一个元素?

I would have thought that it would be defined in your web/app server using JNDI and then look up the JNDI reference in your Java code?

我原以为它会使用 JNDI 在您的 web/app 服务器中定义,然后在您的 Java 代码中查找 JNDI 引用?

The resource-ref definition seems a bit redundant to me and I can't think of when it might be useful. Example:

资源引用定义对我来说似乎有点多余,我想不出它什么时候有用。例子:

<resource-ref>
  <description>Primary database</description>
  <res-ref-name>jdbc/primaryDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>CONTAINER</res-auth>
</resource-ref>

采纳答案by candiru

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.

您始终可以通过容器中配置的 JNDI 名称直接引用应用程序中的资源,但如果这样做,本质上就是将特定于容器的名称连接到代码中。这有一些缺点,例如,如果您以后出于某种原因想要更改名称,则需要更新所有应用程序中的所有引用,然后重新构建和重新部署它们。

<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-specificconfiguration file.

<resource-ref>引入了另一层间接:您在web.xml 中指定要使用的名称,并根据容器在特定容器的配置文件中提供绑定。

So here's what happens: let's say you want to lookup the java:comp/env/jdbc/primaryDBname. The container finds that web.xmlhas a <resource-ref>element for jdbc/primaryDB, so it will look into the container-specific configuration, that contains something similar to the following:

所以这就是发生的事情:假设您要查找java:comp/env/jdbc/primaryDB名称。容器发现web.xml有一个<resource-ref>for 元素jdbc/primaryDB,因此它将查看特定于容器的配置,其中包含类似于以下内容的内容:

<resource-ref>
  <res-ref-name>jdbc/primaryDB</res-ref-name>
  <jndi-name>jdbc/PrimaryDBInTheContainer</jndi-name>
</resource-ref>

Finally, it returns the object registered under the name of jdbc/PrimaryDBInTheContainer.

最后,它返回以 名称注册的对象jdbc/PrimaryDBInTheContainer

The ideais that specifying resources in the web.xmlhas the advantage of separating the developerrole from the deployerrole. In other words, as a developer, you don't have to know what your required resources are actually called in production, and as the guy deploying the application, you will have a nice list of names to map to real resources.

这个想法是在web.xml中指定资源的优点是将开发者角色与部署者角色分开。换句话说,作为开发人员,您不必知道在生产中实际调用了哪些所需资源,而作为部署应用程序的人员,您将有一个很好的名称列表来映射到实际资源。