How To Gear Upwards Java.Io.Notserializableexception: Org.Apache.Log4j.Logger Mistake Inwards Java
java.io.NotSerializableException: org.apache.log4j.Logger fault says that lawsuit of org.apache.lo4j.Logger is non Serializable. This fault comes when nosotros purpose log4j for logging inwards Java in addition to exercise Logger inwards a Serializable degree e.g. whatever domain degree 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 degree every bit fellow member inwards a Serializable class, it volition throw java.io.NotSerializableException Exception.
Look at the below code :
public degree Customer implements Serializable{
private Logger logger = Logger.getLogger(Customer.class)
......
}
If lawsuit of Customer volition hold upwardly 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 in addition to 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 forbid logger lawsuit from default serializabtion process, either arrive transient or static. Making it static in conclusion is preferred selection due to many argue because if you lot arrive transient than afterward deserialization logger lawsuit volition hold upwardly nada in addition to whatever logger.debug() telephone telephone volition number inwards NullPointerException inwards Java because neither constructor non lawsuit initializer block is called during deserialization. By making it static in addition to in conclusion you lot ensure that its thread-safe in addition to all lawsuit of Customer degree tin laissez passer the sack part same logger instance, By the agency this fault is too ane of the argue Why Logger should hold upwardly declared static in addition to final inwards Java program. Just brand next code alter to laid upwardly java.io.NotSerializableException: org.apache.log4j.Logger inwards Java. public degree Customer implements Serializable{
private static in conclusion Logger logger = Logger.getLogger(Customer.class)
......
}
That's all on how to laid upwardly java.io.NotSerializableException: org.apache.log4j.Logger in Java. We convey seen what motility java.io.NotSerializableException: org.apache.log4j.Logger, it's because Logger degree is non Serializable only nosotros too learned that in that place is no hollo for serializing Logger lawsuit in addition to amend to brand Logger static in addition to final.
Further Learning
Complete Java Masterclass
2 ways to solve java.lang.OutOfMemoryError inwards Java
Komentar
Posting Komentar