有没有办法将当前作为依赖项执行的IObjectFactory或ApplicationContext传递给对象?例如
<object id="SpringActionInvoker" type="xxx.SpringActionInvoker, xxx">
<constructor-arg ref="reference_to_the_ApplicationContext_or_ObjectFactor_that_is_executing" />
</object>
我想将它用于我将注入控制器的Asp.Net MVC ControllerActionInvoker的Spring.Net实现
public class SpringActionInvoker : ControllerActionInvoker
{
private IObjectFactory objectFactory;
public SpringActionInvoker(IObjectFactory objectFactory)
{
this.objectFactory = objectFactory;
}
protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
//use objectFactory to inject dependencies into filters
}
}
答案 0 :(得分:4)
让你的类实现IApplicationContextAware
,它可以让你自动注入当前的IApplicationContext。
答案 1 :(得分:0)
怎么样:
<object id="SpringActionInvoker" type="xxx.SpringActionInvoker, xxx" />
然后:
public class SpringActionInvoker : ControllerActionInvoker
{
protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
IObjectFactory objectFactory = ContextRegistry.GetContext();
//use objectFactory to inject dependencies into filters
}
}