什么是web.xml中的resource-ref用于? [英] What is resource-ref in web.xml used for?

查看:252
本文介绍了什么是web.xml中的resource-ref用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

资源引用定义对我来说似乎有点多余,我可以'想想什么时候它可能有用。示例:

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>


推荐答案

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

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

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

所以这里是会发生什么:假设您要查找 java:comp / env / jdbc / primaryDB 名称。容器发现 web.xml 具有 jdbc / primaryDB < resource-ref> 元素$ c>,因此它将调查特定于容器的配置,其中包含类似于以下内容的内容:

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

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

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

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

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

The idea is that specifying resources in the web.xml has the advantage of separating the developer role from the deployer role. 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中的resource-ref用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆