tomcat8资源引用的一些小技巧

tomcat8资源引用的一些小技巧

      原文地址:http://mikusa.blogspot.com/2014/07/tips-on-migrating-to-tomat-8-resources.html

      因为一些原因,原址很多人访问不了,这里写篇译文供大家参考,因为时间原因,这里只粗略翻译文章开头一部分内容,表达主要意思,文章末尾贴上原文供大家详细研究。下面是我的部分翻译:

     Tomcat7Tomcat8的一个主要变化就是Tomcat处理网络应用资源的方法。在tomcat7中,用户可以利用aliasesVirtualloaderVirtualDirContext等特性为自己的APP提供额外的资源支持。但是这些特性都需要分别进行配置使用,而在tomcat8中,都被整合进了resoursces这个统一架构,这样他们的配置更加轻松。

因为这些整合、重构,context配置的一些特性被resources配置代替了。这篇贴士将讨论老本版tomcat7的到新版本tomcat8的在resources配置上的一些转变。

Aliases

tomcat 7中,Context配置里有个特性aliases,它可以让tomcat加载资源时额外加载一系列别的目录。使用aliases有好几个原因,但主要就是为了让用户可以将JAR文件放在WAR文件外的一个集中位置。

下面是aliases一个配置示例:

<Context aliases="/images=/var/www/images" />

下面是tomcat 8resources配置的一个示例:

<Context>

  <Resources>

    <PostResources className="org.apache.catalina.webresources.DirResourceSet"

                   base="/var/www/images" webAppMount="/images" />

  </Resources>

</Context>

后面部分,大家自行翻译看吧,都挺简单的。

下面是原文:

Tips on Migrating to Tomat 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.

Aliases

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.

	
<Context aliases="/images=/var/www/images" />

Here's an example of the same configuration using Tomcat 8's Resources.

<Context>
  <Resources>
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   base="/var/www/images" webAppMount="/images" />
  </Resources>
</Context> 

VirtualDirContext

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.

<Context>
    <Resources className="org.apache.naming.resources.VirtualDirContext"
               extraResourcePaths="/pictures=/Users/theuser/mypictures,/movies=/Users/theuser/mymovies" />
</Context>

?The same configuration under Tomcat 8 would look like this.

<Context>
  <Resources>
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   base="/Users/theuser/mypictures" webAppMount="/pictures" />
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   base="/Users/theuser/mymovies" webAppMount="/movies" />
  </Resources>
</Context>

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.

<Context>
  <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
          virtualClasspath="/apps/shared/lib/*.jar,/apps/shared/classes" />
</Context>

?Here's an example of how this is configured with Tomcat 8.

<Context>
  <Resources>
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   base="/apps/shared/lib" webAppMount="/WEB-INF/lib" />
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   base="/apps/shared/classes" webAppMount="/WEB-INF/classes" />
  </Resources>
</Context>

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.

Additional Information

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.

 

 

 

 

 

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值