开发者

How do you dependency inject beans marked @Configurable after deserialized by Tomcat from SESSIONS.ser?

开发者 https://www.devze.com 2023-02-07 05:14 出处:网络
Note: this won\'t make any sense unless you\'re very familiar with Java, Spring AOP, and Tomcat. The problem is that beans marked @Configurable are not injected when deserialized by Tomcat SESSIONS.s

Note: this won't make any sense unless you're very familiar with Java, Spring AOP, and Tomcat.

The problem is that beans marked @Configurable are not injected when deserialized by Tomcat SESSIONS.ser.

I noticed this behavior on a Struts 1.2.9 based (legacy) application with Spring 2.5.4, spring-tomcat-weaver-2.5.4, Tomcat 6.0.14.

Code:

public class MyForm implements Serializable {
   private Foo myFoo; // getters and setters
}

public class Foo imlements Serializable {
   private Bar myBar; // getters setters
}

@Configurable("barTemplate")
public class Bar implements Serializable {
   @Autowired(required=true)
   private transient SessionFactory hello;
   // other transient dependencies ...
}

The XML configures Bar as a prototype bean.

The correct context:spring-configured and context:load-time-weaver settings are applied, etc (since it works on a cold start of Tomcat).

Everything works fine when starting 开发者_如何学运维for the first time. However, restarting Tomcat causes to write SESSIONS.ser and upon rebooting, to deserialize MyForm, which it does. However, none of the dependencies in Bar are set!

But if I shutdown Tomcat, delete the SESSIONS.ser file, and reboot, then everything will work.

Weird.

Any advice greatly appreciated.


I'll skip the Spring part since I don't do it. I must however admit that I'd also expect that Spring is smart enough to re-inject them after deserialization (edit: this seemed to be fixed in Spring 2.5.2?).

One of the workarounds would be to disable session serialization during Tomcat shutdown/startup. This way you'll start with a fresh new session and everything will just be freshly constructed and injected. The disadvantage is however that the endusers will lose their session data whenever Tomcat restarts.

To achieve this, add a <Manager> element with an empty pathname to the <Context> element of the webapp in question.

<Context ...>
    <Manager pathname="" />
</Context>

This basically instructs Tomcat to use no session manager at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号