java - JSF 国际化 f :loadbundle or through faces-config: Performance point

标签 java jsf jakarta-ee internationalization resourcebundle

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

  1. 全局资源包 全局加载属性文件,以便所有 jsf 页面都可以访问消息。您可以创建“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:本地资源包 在本地加载属性文件,或仅为指定页面加载。声明 <f:loadBundle />需要访问 messages.properties 中消息的页面中的标记。

这两个中哪一个给我的性能更好?

假设我选择第一个选项,这是否意味着所有包都在应用程序启动期间加载,还是延迟加载? (按需)

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

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

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

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

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

最佳答案

Out of these two which one gives me better performance?

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


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?

默认只创建一个。另见 the ResourceBundle API documentation :

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.

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


<application>顺便说一下,声明还有一个额外的好处,即 bundle 也可以通过 @ManagedProperty("#{msg}") 注入(inject)到托管 bean 中。 .另见此问答:Read resource bundle properties in a managed bean .

关于java - JSF 国际化 f :loadbundle or through faces-config: Performance point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14359523/

相关文章:

java - 我们什么时候应该修改通过引用传递的对象?

java - 如何将数据从 Java 客户端发送到 C++ 服务器?

jquery - Richfaces 是否需要 PrototypeScript.js

jakarta-ee - 为什么 Java EE 定时器不集群?

java - tomcat 会与 J2EE 一起运行所有 J2SE 代码吗?

java - 对列表进行排序取决于另一个排序列表,该列表具有一个共同的排序属性

java - 如何编写带有联接的动态 JPA 查询?

java - 验证请求源是来自Internet还是Intranet

java - 将类移动到另一个包后,Netbeans 无法部署 JSF 应用程序

jsf - 如何在JSF中调用带参数的方法