这是因为服务定位器将缓存不论哪个组件首次使用时所重载的名称的资源。
This is because the service locator will cache the resource for whichever component USES the overloaded name first.
无疑地,正如本文所展示的,缓存可以改变定位器的返回结果,因此就改变了应用程序的行为。
Clearly, as this article shows, caching can change the results from the locator and thus the behavior of the application.
当两个组件使用一个重载的资源名,也就是说,一个名称绑定到两个不同的资源,缓存服务定位器为两个组件返回相同的资源。
When two components use a resource name that is overloaded — that is, a name that is bound to two different resources — a caching service locator returns the same resource to both components.
我的第三个实现使用没有缓存的服务定位器。
My third implementation used a service locator with no cache.
重载资源名使得服务定位器与其缓存之间变得混乱。
So overloaded resource names mess up a service locator and its cache.
但是服务定位器不应包含资源缓存。
But the service locator should not include a resource cache.
不要只假设缓存服务定位器能显著的提高应用程序的性能,要使用性能测试来证明这一点。
Do not just assume that a caching service locator significantly improves application performance; use performance testing to confirm that it does.
我们已经看到了重载资源名和典型服务定位器实现,有资源缓存的集合,但他们之间并没有很好的合作。
We have seen that an overloaded resource name and the typical service locator implementation, a singleton with a resource cache, do not get along.
服务定位器实现通常包括资源缓存,以此来避免对相同资源的重复查找。
Service locator implementations usually include a resource cache to avoid repeated lookups of the same resource.
因此,在J2EE 1.3应用程序中,服务定位器的实现不应该包含资源缓存。
Therefore, J2EE 1.3 applications should not include the resource cache in their service locator implementations.
将其应用于现有应用程序很简单:只要改变服务定位器实现去禁止或去除缓存。
This is simple to apply to an existing application: just change the service locator implementation to disable or remove the cache.
缓存引用的服务定位器将导致J2EE 1.3(以及以后版本)中有重载资源名的应用程序工作不正常。
A service locator that caches references will cause a J2EE 1.3 (and later) application with an overloaded resource name to work incorrectly.
我第二个实现使用了缓存服务定位器。
包含缓存服务定位器的应用程序迟早要出问题,而且这个问题将很难检测、很难再现且很难诊断。
Applications that contain a caching service locator are a problem waiting to happen — a problem that will be difficult to detect, difficult to reproduce reliably, and difficult to diagnose.
并不是要改变定位器的行为,结果必须是相同的或没有缓存。
It's not supposed to change the behavior of the locator; the results must be the same with or without caching.
包含访问数据源缓存的服务定位器实现如下。
A service locator implementation that includes caching accesses a data source like this.
由于缓存服务定位器的原因,两个bean现在使用相同数据源。
Because of the caching service locator, the two beans are now using the same data source.
ServiceLocator模式对封装使用JNDI的代码仍然有用,即使定位器不使用缓存。
The Service Locator pattern is still useful for encapsulating the code that uses JNDI, even if the locator does not use caching.
简单的结论是:使用服务定位器进行缓存不是一个好的主意。
The simple conclusion is that service locators that cache are a bad idea.
带有资源缓存的集合服务定位器创建全局缓存,通过这种方式不能为重载的资源名正确处理组件级映射。
A singleton service locator with a resource cache creates a global cache that cannot properly handle the component-level mappings for an overloaded resource name.
组件可以通过根本不使用服务定位器而避免重载缓存的名称的问题,但是这种方法又有点错杀一千的感觉。
Components could avoid the problem of overloaded names being cached by not using service locators at all, but that would be overkill.
好,既然是由于缓存服务定位器导致重载资源名时的错误。
OK, so a caching service locator causes errors for overloaded resource names.
服务定位器代码两次查找资源,因为第一次查找没有被缓存起来。
The service locator's code looks up the resource both times because the first lookup is not cached. Here is the output
服务定位器代码两次查找资源,因为第一次查找没有被缓存起来。
The service locator's code looks up the resource both times because the first lookup is not cached. Here is the output
应用推荐