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 --开发者_JS百科>
</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?
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".
精彩评论