How To Cook Java.Io.Notserializableexception: Org.Apache.Log4j.Logger Fault Inwards Java
Monday, June 11, 2018
Add Comment
java.io.NotSerializableException: org.apache.log4j.Logger fault says that lawsuit of org.apache.lo4j.Logger is non Serializable. This fault comes when nosotros role log4j for logging inwards Java as well as practise Logger inwards a Serializable aeroplane e.g. whatsoever domain aeroplane or POJO which nosotros desire to shop inwards HttpSession or desire to serialize it. As nosotros know from 10 Java Serialization interview question that, if you lot convey a non serializable aeroplane every bit fellow member inwards a Serializable class, it volition throw java.io.NotSerializableException Exception.
Look at the below code :
public aeroplane Customer implements Serializable{
private Logger logger = Logger.getLogger(Customer.class)
......
}
If lawsuit of Customer volition endure stored inwards HttpSession or Serialized externally it volition throw "java.io.NotSerializableException: org.apache.log4j.Logger" because hither logger lawsuit is neither static or transient as well as it doesn't implement Serializable or Externalzable interface.
How to solve java.io.NotSerializableException: org.apache.log4j.Logger
Solving java.io.NotSerializableException: org.apache.log4j.Logger is simple, you lot convey to foreclose logger lawsuit from default serializabtion process, either piece of job into transient or static. Making it static end is preferred pick due to many argue because if you lot piece of job into transient than later on deserialization logger lawsuit volition endure zippo as well as whatsoever logger.debug() telephone vociferation upwards volition effect inwards NullPointerException inwards Java because neither constructor non lawsuit initializer block is called during deserialization. By making it static as well as end you lot ensure that its thread-safe as well as all lawsuit of Customer aeroplane tin portion same logger instance, By the agency this fault is likewise 1 of the argue Why Logger should endure declared static as well as final inwards Java program. Just brand next code alter to gear upwards java.io.NotSerializableException: org.apache.log4j.Logger inwards Java.
public aeroplane Customer implements Serializable{
private static end Logger logger = Logger.getLogger(Customer.class)
......
}
That's all on how to gear upwards java.io.NotSerializableException: org.apache.log4j.Logger in Java. We convey seen what drive java.io.NotSerializableException: org.apache.log4j.Logger, it's because Logger aeroplane is non Serializable only nosotros likewise learned that at that topographic point is no quest serializing Logger lawsuit as well as meliorate to brand Logger static as well as final.
Further Learning
Complete Java Masterclass
2 ways to solve java.lang.OutOfMemoryError inwards Java
0 Response to "How To Cook Java.Io.Notserializableexception: Org.Apache.Log4j.Logger Fault Inwards Java"
Post a Comment