[英]CDI: Why does my transient field gets serialized?
With CDI (Weld) I implemented a producer method for a non serializable class. 使用CDI(焊接),我为不可序列化的类实现了生产者方法。 If the producer got serialized and the field NonSerializableObject is gone, I reinitialize a NonSerializableObject:
如果生产者被序列化并且字段NonSerializableObject不存在,我将重新初始化NonSerializableObject:
@SessionScoped
public class NonSerializableObjectProducer implements Serializable {
/** UUID */
private static final long serialVersionUID = -436472364723647647367L;
/** my transient non serializbale object */
private transient NonSerializableObject nonSerializableObject;
@SessionScoped
@Produces
protected NonSerializableObject produceNonSerializableObject() {
if (nonSerializableObject == null) {
// reinitialize object and set it
// nonSerializableObject = ...
}
return nonSerializableObject;
}
I inject a NonSerializableObject like: 我注入NonSerializableObject像:
@SessionScoped
public class UseSerializableObject implements Serializable {
private static final long serialVersionUID = 62378785678643784689L;
@Inject
private transient NonSerializableObject nonSerializableObject;
public void doSomething() {
nonSerializableObject.go();
}
}
When my application server (Tomcat 7) redeploys the following exception occurs: 当我的应用程序服务器(Tomcat 7)重新部署时,会发生以下异常:
Sep 22, 2014 4:41:58 PM org.apache.catalina.session.StandardSession writeObject
WARNING: Cannot serialize session attribute WELD_S#5 for session 4694BB90FF1BBD8
java.io.NotSerializableException: com.MemberOfNonSerializableObject
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1181)
So, a member variable of my NonSerializableObject gets serialized. 因此,我的NonSerializableObject的成员变量被序列化。 Why?
为什么? I declared everything as transient!
我宣布一切都是暂时的!
Your method: 您的方法:
@SessionScoped
@Produces
protected NonSerializableObject produceNonSerializableObject() {}
produces @SessionScoped
object, which is not serializable. 产生
@SessionScoped
对象,该对象不可序列化。 All @RequestScoped, @SessionScoped, and @ApplicationScoped beans must be Serializable. 所有@ RequestScoped,@ SessionScoped和@ApplicationScoped Bean必须可序列化。
So it would work, if you would removed @SessionScoped from the producer method. 因此,如果您要从生产者方法中删除@SessionScoped,它将可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.