tomcat8资源引用的一些小技巧
原文地址:http://mikusa.blogspot.com/2014/07/tips-on-migrating-to-tomat-8-resources.html
因为一些原因,原址很多人访问不了,这里写篇译文供大家参考,因为时间原因,这里只粗略翻译文章开头一部分内容,表达主要意思,文章末尾贴上原文供大家详细研究。下面是我的部分翻译:
Tomcat7到Tomcat8的一个主要变化就是Tomcat处理网络应用资源的方法。在tomcat7中,用户可以利用aliases、Virtualloader和VirtualDirContext等特性为自己的APP提供额外的资源支持。但是这些特性都需要分别进行配置使用,而在tomcat8中,都被整合进了resoursces这个统一架构,这样他们的配置更加轻松。
因为这些整合、重构,context配置的一些特性被resources配置代替了。这篇贴士将讨论老本版tomcat7的到新版本tomcat8的在resources配置上的一些转变。
Aliases
在tomcat 7中,Context配置里有个特性aliases,它可以让tomcat加载资源时额外加载一系列别的目录。使用aliases有好几个原因,但主要就是为了让用户可以将JAR文件放在WAR文件外的一个集中位置。
下面是aliases一个配置示例:
下面是tomcat 8里resources配置的一个示例:
后面部分,大家自行翻译看吧,都挺简单的。
下面是原文:
One of the major changes from Tomcat 7 to Tomcat 8 was a refactoring with how Tomcat handles web application resources. With Tomcat 7, there are features like aliases, VirtualLoader and VirtualDirContext that provide admins with a way to pull external resources into an app. Unfortunately, each one of these features was implemented separately so to consolidate things and make them easier to maintain in Tomcat 8, all of these different implementations were combined into one resources framework which now handles all of that functionality.
Because of the consolidation and refactoring, several attributes from the Context configuration were removed and replaced with the Resources configuration. This migration tip discusses how to switch from using common variations of the old Tomcat 7 configuration to the new Tomcat 8 Resources configuration.
In Tomcat 7, the Context configuration tag has an attribute called aliases which can be used to provide a list of external locations from which Tomcat will load resources for the context. There were a few reasons to use this, but one common reason was to use it to include static resources or configuration that an administrator wanted to place outside of a WAR file.
Here's an example configuration using aliases.
Here's an example of the same configuration using Tomcat 8's Resources.
The VirtualDirContext feature is similar to aliases but was targeted as a development feature. The suggestion for using it is so that you do not have to deploy an entire WAR file. Instead, you can point to the location of your static resources in your project and only copy, zip and deploy the code itself.
Here is an example of how this was used under Tomcat 7. In this case, I'm using it to point to picture and movie assets that need to be included with the application.
?The same configuration under Tomcat 8 would look like this.
VirtualWebappLoader
It's also possible to use a PreResources tag, instead of the PostResources. The difference would be that resources listed with a PostResources tag are checked after resources bundled in the web applications and resources with the PreResources tag are checked prior to resources bundled in the web application. In other words, PreResources can be used to override a resource that is included with an application.
The VirtualWebappLoader feature is used to add additional directories or JAR files onto the class path. This is different from the previous two options because it allows you to add JAR files, classes or configuration to the class path and not just the file system. Again, there are a few reasons why you might want to do this, one common reason was so that you could pull JAR files out from the WAR file to a centralized location.
Here's an example of how this was configured with Tomcat 7.
?Here's an example of how this is configured with Tomcat 8.
If you were using the searchExternalFirst attribute of the VirtualWebappLoader class, switching from using a PostResource tag to a PreResource tag should allow you to achieve the same behavior.
That covers some of the common configuration scenarios that have been changed due to the new Resources API. For more information on this change, I would suggest the following resources.
· Resources Configuration Docs
· Migration Guide - Web Application Resources
· WebResourceRoot JavaDoc
Feel free to drop me a comment if I missed anything.