【发布时间】:2015-10-03 19:30:35
【问题描述】:
首先,对不起我的英语不好!
在以下托管 Bean (ApplicationScoped) 中,我将 ResourceBundle(.properties) 作为 @ManagedProperty 访问。 ResourceBundle 对象不可序列化,因此我在 Eclipse/Tomcat 控制台中收到一个错误消息,指出该对象无法序列化/反序列化.. 等等。
从持久存储中加载异常会话 java.io.WriteAbortedException:写入中止; java.io.NotSerializableException: java.util.PropertyResourceBundle
我对这个问题有 2 个问题:
- 我认为,JSF 将预定义(在 faces-config.xml 中)
ResourceBundles处理为 ApplicationScoped bean。这意味着(如果我理解正确的话),这个 Object/Bean(ResourceBundle)以某种方式存储在文件中的某个地方(持久存储)。现在,既然ResourceBundle不可序列化,那么它以哪种格式存储? JSF 如何为这样的“Beans”服务?序列化的 Objects 以 Bytes 的形式存储在文件中,那么不可序列化的 Objects 是如何存储的呢? - 在下面的示例中,我将我的
@ManagedProperty ResourceBundle声明为transient(由于序列化问题),但是瞬态对象不会存储在持久存储中(无状态),这是否意味着每次调用方法getConfigurationAttribute(我在哪里使用这个resourceBundle)都会重新创建/重新加载ManagedPropery ResourceBundle,因为它被标记为瞬态?
非常感谢您的帮助。
@ManagedBean(name="facesResource",eager=true)
@ApplicationScoped
public class FacesResource implements Serializable{
private static final long serialVersionUID = 2454454363100273885L;
@ManagedProperty("#{FACES_CONFIG}")
private ResourceBundle facesConfig;
//private transient ResourceBundle facesConfig;
....
private Map<String,Language> languagesMap;
private Map<String,Theme> themesMap;
....
public FacesResource(){
}
@PostConstruct
public void init(){
System.out.println("*** FacesResource init ....");
try{
....
this.initLanguages();
this.initThemes();
....
}catch(Exception ex){
ex.printStackTrace();
}
}
public String getConfigurationAttribute(String attributeKey){
return this.facesConfig.getString(attributeKey);
}
// ... other methods & getter/setter ...etc
}
更新:
FacesResource Bean 中的 ResourceBundle 独立 独立于请求
Locale,因此在 ApplicationScoped Bean 中加载它不是问题,但是因为我在其他 SessionScoped Bean 中访问/注入(作为 @ManagedProperty)这个 ApplicationScoped Bean,它应该被序列化,这意味着所有属性(包括资源包)也应该被序列化,这里我遇到了问题带序列化/反序列化
@BalusC:如果我确实喜欢你在回答中的建议:
ResourceBundle.getBundle("com.example.text"),我必须提供捆绑包的基本名称。但这正是我想要避免的。我不想在 java 源代码中硬编码静态路径),所以当路径发生变化时(最不可能,但对于这种情况),我不喜欢在 Java 源代码中更改路径,而只在 faces-config 中更改路径。 xml。我不能使用
FacesContext.getCurrentInstance().getApplication().getResourceBundle(facesContext, "bundleVarName");,因为我的 Bean 被标记为 eager=true,这意味着,facesContext目前是 NULL!
【问题讨论】:
标签: jsf serialization jsf-2.2 resourcebundle transient