在Spring中创建对象池

发布于 2024-08-26 00:28:40 字数 281 浏览 6 评论 0 原文

我有一个类,

class ObjPool {
 MyObject getObject() {...}
 void returnObject() {...}
 int getUsedCount() {...}
}

我们如何使用 Spring 框架,以便它只向所有(Web)应用程序提供该工厂的一个副本,以便每个应用程序都使用相同的 ObjPool?

我的意思是,如果应用程序 A 从此 ObjPool 获取一个对象,那么调用 getUsedCount() 的所有其他应用程序都会看到该值递减。

I have a class


class ObjPool {
 MyObject getObject() {...}
 void returnObject() {...}
 int getUsedCount() {...}
}

How can we use Spring framework so that it would provide only one copy of this factory to all (web) applications and so that each and every application use the same ObjPool?

I mean if app A gets an object from this ObjPool, then all other applications that invoke getUsedCount() will see that value decremented.

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-09-02 00:28:40

在 Spring 中,默认情况下每个 bean 都是单例,这意味着 - 每个 ApplicationContext 有一个 bean 实例。这不是每个容器一个,而是每个 Web 应用程序一个。

http://blog.springsource.com/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/ 给出了一个例子如何在 EAR 级别加载 ApplicationContext 并在所有战争中共享。

In Spring each bean is a singleton by default, which means - one instance of the bean per ApplicationContext. This is NOT one per container, but one per web application.

http://blog.springsource.com/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/ gives an example of how to load an ApplicationContext at the EAR level and share across all wars.

萌能量女王 2024-09-02 00:28:40

你可以只谷歌搜索“spring导出到jndi”

http://maestro-lab.blogspot.com/2009/01/how-to-export-spring-management-bean-to.html

org.springframework.jndi.JndiTemplate 是一个很好的模板开始的地方

You could just google for "spring export to jndi"

http://maestro-lab.blogspot.com/2009/01/how-to-export-spring-managed-bean-to.html

org.springframework.jndi.JndiTemplate is a good place to start

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