1

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.

les2
  • 14,093
  • 16
  • 59
  • 76

1 Answers1

1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i tested setting the pathname and it worked (prevented tomcat from restoring the session from SESSIONS.ser). – les2 Jan 31 '11 at 04:34
  • we seem to be using spring 2.5.4 ... i saw that bug as well when researching the problem for myself. it seems to apply only to when preConstruction=true is set on @Configurable (meaning that dependencies will be injected before the constructor is invoked - kind of weird, eh?). – les2 Jan 31 '11 at 04:37