JSF国际化f:loadbundle或通过faces-config:性能点 [英] JSF Internationalization f:loadbundle or through faces-config: Performance point

查看:114
本文介绍了JSF国际化f:loadbundle或通过faces-config:性能点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法可以将属性文件加载到JSF 2.0中。

There are two ways to load the properties file into JSF 2.0.


  1. 全局资源包
    加载属性文件全局,以便所有jsf页面都可以访问消息。您可以创建faces-config.xml文件并显式声明属性文件。

faces-config.xml

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
     <application>
      <resource-bundle>
        <base-name>com.mkyong.messages</base-name>
        <var>msg</var>
       </resource-bundle>
     </application>
</faces-config>

选项2:本地资源包
加载属性本地文件,或仅用于指定页面。在需要访问messages.properties中的消息的页面中声明< f:loadBundle /> 标记。

在这两个中哪一个给了我更好的表现?

Out of these two which one gives me better performance?

让我说我选择第一个选项,是吗?这意味着在应用程序启动期间所有的bundle都被加载了还是延迟加载? (按需)

Lets say I going with 1st option, does it means all the bundles gets loaded during the application startup or is it lazy loading? (on demand)

如果选择第二个选项,是否可能导致每个ViewRoot多次加载包?

If choose a 2nd option, does it potentially cause bundle to get loaded multiple time for each ViewRoot?

Java ResourceBundle是工厂类,它在servlet容器中提供单例对象吗?

Is the Java ResourceBundle is factory class which provides singleton object within servlet container?

我的意思是getBundle方法是工厂方法,它总是创建单例对象?

I mean getBundle method is factory method which creates singleton object for always?

ResourceBundle myResources =
      ResourceBundle.getBundle("MyResources", currentLocale);

假设我有一个页面abc.xhtml,我使用的是f:loadBundle,还有1000个用户访问此页面,这是否意味着将创建1000个resouceBundle对象?或者它只是所有页面实例共享的对象?

Lets say I have a page abc.xhtml and I am using f:loadBundle, and there 1000 users accessing this page, does this mean there would 1000 resouceBundle object created? or is it only object which is being shared by all the page instances?

推荐答案


这两个中有哪一个让我有更好的表现?

我不担心性能。 ResourceBundle 已在内部缓存它们。

I wouldn't worry about the performance. The ResourceBundle already caches them internally.


让我说我有一个页面abc.xhtml而我正在使用f :loadBundle,有1000个用户访问这个页面,这是否意味着会创建1000个resouceBundle对象?或者只是所有页面实例共享的对象?

默认情况下,只创建一个。另请参见 ResourceBundle API文档

By default, only one is created. See also the ResourceBundle API documentation:


缓存管理



默认情况下,getBundle工厂方法创建的资源包实例会被缓存,如果已缓存,则工厂方法会多次返回相同的资源包实例。 getBundle客户端可以清除缓存,使用生存时间值管理缓存资源包实例的生存期,或指定不缓存资源包实例。请参阅 getBundle 工厂方法 clearCache ResourceBundle.Control.getTimeToLive ResourceBundle.Control.needsReload 有关详细信息。

Cache Management

Resource bundle instances created by the getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached. getBundle clients may clear the cache, manage the lifetime of cached resource bundle instances using time-to-live values, or specify not to cache resource bundle instances. Refer to the descriptions of the getBundle factory method, clearCache, ResourceBundle.Control.getTimeToLive, and ResourceBundle.Control.needsReload for details.

您可以通过查看实例的哈希码轻松证明自己是否在调试器中。

You can easily testify if yourself in debugger by looking at instance's hashcode.

< application> 声明具有捆绑包也可注入的额外好处 @ManagedProperty(#{msg})的托管bean。另请参阅此问与答:阅读资源包属性托管bean

The <application> declaration has by the way the additional benefit that the bundle is also injectable in a managed bean by @ManagedProperty("#{msg}"). See also among others this Q&A: Read resource bundle properties in a managed bean.

这篇关于JSF国际化f:loadbundle或通过faces-config:性能点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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