开发者

Can I use Spring.NET to inject dependencies in a created instance?

Is it possible to to something like this using Spring.NET IoC container?

MyClass instance = new MyClass();
ContextRegistry.GetContext().InjectDepenencie开发者_运维问答s(instance);
// instance has the defined dependencies set

This would come in handy. Of couse I could use ContextRegistry.GetContext().Get("name") but that would create a new instance of the defined object. I would need to set the dependencies of an already created object.


There are three options available, the first one matches what you want.

  • IApplicationContext.ConfigureObject(object target, string name)

    This configures the target object using the object definition which is matched by the name argument.

  • IApplicationContext.Get(string name, object[] arguments)

    Which will either use the constructor or a static factory method which will receive the arguments as specified.

  • GenericApplicationContext.RegisterObjectDefinition(string name, IObjectDefinition definition)

    You can use it to register dependencies at runtime.


ContextRegistry.GetContext().Get("name") will not create a new instance if name is defined as singleton which is the default scope in spring.net.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜