有些需要特别注意的地方如下:
1.)web.xml中添加的元素的顺序不能改变。
2.)在server.xml中添加<context>元素,和在某个web应用对应的 .xml文件中添加<context>元素都可以访问配置好的资源。Tomcat会先检查server.xml,如果两个有相同的配置的话,Tomcat会以server.xml中的为准,并且删除该应用对应的.xml文件,然后产生一个新的。这个新的只包含一个<context>根元素,不包含<resource>元素及其中的其他元素。
3.)资源名称(例子中的是bean/MyBeanFactory,即<Resource>元素中的name属性的值)必须和web.xml中指定的<resource-env-ref-name>的值相同。另外,在资源工厂的参数配置中可以指定JavaBean的属性的初始值。示例代码中就把Bar属性的值设为23了。(JavaBean中Bar的默认值是0).不指定的话,就使用程序中设置的初始值.
原文:
Note that the resource name (here, bean/MyBeanFactory must match the value specified in the web application deployment descriptor. We are also initializing the value of the bar property, which will cause setBar(23) to be called before the new bean is returned. Because we are not initializing the foo property (although we could have), the bean will contain whatever default value is set up by its constructor.