Spring.Net 中的多个上下文

发布于 2024-12-02 11:20:16 字数 717 浏览 1 评论 0原文

spring.Net 中是否可以并行拥有多个上下文,而无需在调用其他上下文时创建它们的对象?

<spring>
 <context>
  <context name="A">
   <!-- ... some objects might be created here -->
  </context>
  <context name="B">
   <!-- ... some objects might be created here -->
  </context>
 </context>
</spring>

Spring.Net 中奇怪的事情是,即使我为特定上下文调用 GetContext() (例如GetContext("A"))所有对象(即使我调用 A 时来自 B 的对象)都会被创建。

var ctx = ContextRegistry.GetContext("A");
var my = (MyClass)ctx.GetObject("MyObject"); // where MyObject is in context A

我可以在调用 GetObject() 时显式执行延迟初始化,但可能有更好的解决方案吗?

Is it possible to have multiple contexts in spring.Net in parallel without creating their objects when calling the other context?

<spring>
 <context>
  <context name="A">
   <!-- ... some objects might be created here -->
  </context>
  <context name="B">
   <!-- ... some objects might be created here -->
  </context>
 </context>
</spring>

The weird thing in Spring.Net is that even if I call GetContext() for a specific context
(GetContext("A") for example) all objects (even those from B if I call A) are created.

var ctx = ContextRegistry.GetContext("A");
var my = (MyClass)ctx.GetObject("MyObject"); // where MyObject is in context A

I could explicitly do a lazy initialization when calling GetObject() but there might be a better solution out there?

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

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

发布评论

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

评论(1

风蛊 2024-12-09 11:20:16

默认情况下,当上下文初始化并在应用程序启动时添加到注册表时,应该立即实例化单例,因此我非常确定上下文“B”中的非惰性对象早在您调用 GetObject( )在任一上下文上(即,急切实例化与您进行任何 GetObject() 调用 IIRC 时完全无关)。

AFAIK,完成您所追求的任务的唯一方法确实是将整个上下文“B”的默认lazy设置为true,或者以其他方式在上下文“B”中逐个对象地指示lazy=true 。

By default, singletons should be eagerly-instantiated when the context is initialized and added to the registry as your app starts up so I'm pretty certain that the non-lazy objects from Context "B" are already instantiated long before you call GetObject() on either Context (i.e., eager instantiation isn't at all related to when you make any GetObject() calls IIRC).

AFAIK, the only way to accomplish what you're after is indeed to either set the default lazy to true for the entirety of Context "B" or to otherwise indicate lazy=true on an object-by-object basis within Context "B".

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