first of all, sorry for my bad english!
in the following managed Bean (ApplicationScoped), i access a ResourceBundle(.properties) as a @ManagedProperty. a ResourceBundle Object is not serializable, so i get in the Eclipse/Tomcat Console an Error saying that this object cannot be serialized/de-serialized.. etc.
Exception loading sessions from persistent storage java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.util.PropertyResourceBundle
i have 2 Questions to this issue:
- i think, JSF handles pre-defined(in faces-config.xml)
ResourceBundles
as ApplicationScoped beans. this means(if i understanding this correctly), this Object/Bean (ResourceBundle) is been stored somewhere somehow in a file (persistent storage). Now and sinceResourceBundle
is not serializable, then in which format is it been stored? and how JSF serves such "Beans"? serialized Objects are stored in files as Bytes, so how not serializable Objects are stored? - in the following example, i would declare my
@ManagedProperty ResourceBundle
as transient (due to serialization problem), but transient objects won't be stored in persistent storage (stateless), does this mean that with every call of the methodgetConfigurationAttribute
(where i use this resourceBundle) will recreate/reload the ManagedPropery ResourceBundle since it is marked as transient?
Your help is greatly appreciated.
@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
}
UPDATE:
the ResourceBundle in the FacesResource Bean is independent of the Request
Locale
, so its not a problem to load it in an ApplicationScoped Bean, BUTsince i access/inject(as @ManagedProperty) this ApplicationScoped Bean in other SessionScoped Beans, which should be serialized, which means, that all attributes (resourceBundle included) should be serialized too, and here i got the Problem with Serialization/Deserializazion
@BalusC: if i do like you suggest in your answer:
ResourceBundle.getBundle("com.example.text")
, i have to provide the baseName of the Bundle. BUT this is exactly what i want to avoid. i don't want to hardcode static Paths in java Source Codes), so that when the path changes(most unlikely, but for the Case), i don't like to change paths in Java Source Codes but only in faces-config.xml.and i cannot use
FacesContext.getCurrentInstance().getApplication().getResourceBundle(facesContext, "bundleVarName");
because my Bean is marked with eager=true, which means, thefacesContext
is NULL at this moment!