Tomcat 从 SESSIONS.ser 反序列化后,如何依赖注入标记为 @Configurable 的 bean?

发布于 2024-10-14 15:53:33 字数 943 浏览 3 评论 0原文

注意:除非您非常熟悉 Java、Spring AOP 和 Tomcat,否则这没有任何意义。

问题在于,当 Tomcat SESSIONS.ser 反序列化时,标记为 @Configurable 的 bean 不会被注入。

我在带有 Spring 2.5.4、spring-tomcat-weaver-2.5.4、Tomcat 6.0.14 的基于 Struts 1.2.9(旧版)应用程序上注意到了这种行为。

代码:

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 ...
}

XML 将 Bar 配置为原型 bean。

应用正确的 context:spring-configured 和 context:load-time-weaver 设置等(因为它在 Tomcat 冷启动时工作)。

第一次启动时一切正常。但是,重新启动 Tomcat 会导致写入 SESSIONS.ser,并在重新启动时反序列化 MyForm,它确实会这样做。但是,Bar 中没有设置任何依赖项!

但如果我关闭 Tomcat,删除 SESSIONS.ser 文件,然后重新启动,那么一切都会正常。

诡异的。

任何建议都非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-10-21 15:53:33

我将跳过 Spring 部分,因为我不这样做。然而,我必须承认,我也期望 Spring 足够聪明,能够在反序列化后重新注入它们(编辑:这似乎是 在 Spring 2.5.2 中已修复?)。

解决方法之一是在 Tomcat 关闭/启动期间禁用会话序列化。这样,您将从一个全新的会话开始,并且所有内容都将是全新构建和注入的。然而,缺点是每当 Tomcat 重新启动时,最终用户都会丢失其会话数据。

要实现此目的,请将带有 pathname 元素添加到 元素有问题的网络应用程序的。

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

这基本上指示 Tomcat 根本不使用会话管理器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文