When you deserialize an object in spring, you need to call the Readresolve method to verify the integrity of the object. For JAVA6 locale objects, the specific implementation
Private Object Readresolve () throws Java.io.ObjectStreamException { return getinstance (language, country, variant) ;}
No problem. But for JAVA7, the 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 calls 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 via ReadObject (Abstracthessianinput in, Object obj,string[] FieldNames) assigns a value to the field of this object and does not construct by invoking the new method of the object, which is not constructed by the Java general construction method. So here's the problem.
In Java7, the Baselocale field of the locale is a transient field that is not serialized and is dynamically constructed in the object construction method based on several other fields.
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, with the implementation of spring above, the Baselocale field of the locale object calling the Readresolve object cannot be initialized, only null, so
Return getinstance (Baselocale.getlanguage (), Baselocale.getscript (), baselocale.getregion (), Baselocale.getvariant (), localeextensions);
The null pointer exception must be thrown here.
A bug in the JAVA7 environment where the spring container deserializes the locale object.