When you deserialize an object in spring, you need to call the Readresolve method to verify the integrity of the object. For the locale object of JAVA6, the concrete implementation
Private Object Readresolve () throws Java.io.ObjectStreamException {return
getinstance (language, country, variant) ;
}
No problem. But for JAVA7 's real
Private Object Readresolve () throws Java.io.ObjectStreamException {return
getinstance (baselocale.getlanguage), Baselocale.getscript (), Baselocale.getregion (), Baselocale.getvariant (),
localeextensions);
We know that the locale object that invokes the Readresolve method is the Hessian implementation in the spring framework, is generated directly in memory using the Sun.misc.UnSafe Allocateinstance method, and then through ReadObject (Abstracthessianinput in, Object obj,string[) FieldNames) assigns a value to the field of this object and does not construct it by invoking the new method of the object, which is plainly not constructed by means of the Java general construction method. So here's the problem.
In Java7, the locale baselocale field is a transient field that is not serialized and is dynamically constructed from several other fields in the object's construction method.
Public Locale (String language, String country, string variant) {
if (language== null | | country = NULL | | | variant = = NULL) {
throw new NullPointerException ();
}
Baselocale = Baselocale.getinstance (convertoldisocodes (language), "", country, variant);
Localeextensions = getcompatibilityextensions (Language, "", country, variant);
}
So through the implementation of spring above, the Baselocale field of the locale object calling the Readresolve object cannot be initialized, it can only be null, so
Return getinstance (Baselocale.getlanguage (), Baselocale.getscript (),
baselocale.getregion (), Baselocale.getvariant (), localeextensions);
This will definitely throw a null pointer exception.
The repair of this problem does not go to hack hessian implementation, do not put locale directly into the serialization of deserialization, the best conversion is also string, when taken out and then back to locale according to the string.