使用“请求”注入对象范围到原型对象
我在我的 Asp.Net MVC 应用程序中使用 Spring.Net,其中控制器必须定义为原型(非单例)。我有必须具有请求范围的对象(每个请求的新对象)。有没有办法将它们注入我的控制器?
<object type="xx.CompanyController, xx" singleton="false">
<property name="Service" ref="ServiceA" />
</object>
<object id="ServiceA" type="xx.ServiceA, xx" scope="request"/>
<property name="ObjectB" ref="ObjectB" />
</object>
<object id="ObjectB" type="xx.ObjectB, xx" scope="request"/>
像这样,除了控制器之外的所有对象都被视为单例。 ObjectB 不能是原型,因为它被需要共享同一实例的其他一些对象引用。从控制器中删除 singleton="false" 并添加scope="request" 也不起作用(控制器被视为单例)。
我将 Spring.Net 1.3.1 与 MvcApplicationContext 一起使用
I use Spring.Net in my Asp.Net MVC application where controllers must be defined as prototypes (non singleton). I have objects that must have a request scope (new object for each request). Is there a way to inject them into my controller?
<object type="xx.CompanyController, xx" singleton="false">
<property name="Service" ref="ServiceA" />
</object>
<object id="ServiceA" type="xx.ServiceA, xx" scope="request"/>
<property name="ObjectB" ref="ObjectB" />
</object>
<object id="ObjectB" type="xx.ObjectB, xx" scope="request"/>
Like this all objects except the controllers are treated as singleton.
ObjectB must not be prototype beause it is referenced by some other objects that need to share the same instance. Removing singleton="false" from the controller and add scope="request" also doesn't work (controller is treated as singleton).
I use Spring.Net 1.3.1 with MvcApplicationContext
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将 ControllerFactory 设置为您自己的。
IoCControllerFactory 必须继承自 DefaultControllerFactory
并重写 GetControllerInstance
IoCWorker 是一个具有用于注入的 Resolve 方法的类,我正在使用 Unity,我无法提供 IoCWorker 实现 - 如果您需要我可以共享 - 。
它可以工作 ctor 注入为您的控制器。
祝你好运。
You should set ControllerFactory with your own.
IoCControllerFactory must inherit from DefaultControllerFactory
And override GetControllerInstance
IoCWorker is a class has Resolve method for injection, I am using Unity and I could not give IoCWorker implementation -if you need i can share-.
It will work ctor inject for your controller.
Good luck.