试图寻找,但没有具体的答案(我很新在此)Spring.NET DI注射...如何做动作过滤器(ASP.NET MVC 3)
使用Spring.NET支持ASP。 NET MVC 3,如何在动作过滤器中执行依赖注入?研究过,但我根本不知道! :/
在Spring.NET纪录片上,它似乎提到这一切都已经被处理,所需做的只是将动作过滤器注册到spring的上下文中,并且spring将处理所有必要的注入处理。
http://www.springframework.net/doc-latest/reference/html/web-mvc3.html
不过,我只是无法得到它的工作。围绕学习和博客等,在使用过滤器供应商,或直接使用ContextRegistry.GetContext()的提及,但是这似乎并没有坚持AOP ..
Using Spring.Net to inject dependencies into ASP.NET MVC ActionFilters
Spring.Net & Attribute Injection
所以什么是这样做的正确方法?我认为这已经被spring.net覆盖了,但它似乎并不是。
我到Spring.Web.Mvc3 v4.0.30319引用的,而我在ASP.NET MVC 3运行
编辑:
我尝试这样做:
SpringMvcDependencyResolver dr =
new SpringMvcDependencyResolver(ContextRegistry.GetContext());
然后通过dr.GetService<ITestService>();
获得服务这似乎使它有点工作,但不是获得正确的实际服务实现(在我的Filters.xml中,我写了属性ref为TestService
),它返回th e服务的存根版本。我做错了吗?或者我必须筛选并通过GetServices()
选择合适的人或某物?
EDIT2:
@Andreas:很抱歉这么晚才回复。但是,我Global.asax
确实有它
public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication
为了测试,我有这个在我的Spring配置:
<object type="Spring.Mvc3QuickStart.Filters.TestFilter, Spring.Mvc3QuickStart" singleton="false" >
<property name="Message" value="This is the ActionFilter message" />
</object>
在我的过滤器:
public class TestFilter : ActionFilterAttribute
{
public string Message { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.ActionMessage = Message;
base.OnActionExecuting(filterContext);
}
}
在我的控制器我有这个:
ViewData["Message"] = Message;
纯粹为了这个测试项目,过滤器应该注入消息。但产出并不如预期。
任何帮助吗? :) – RicL
你可以请发布你的弹簧配置的相关部分?当你从SpringMvcApplication派生你的global.asax时,prop注入动作过滤器可以工作。 – Andreas
@安德里亚斯:编辑回答你的问题:) 嗯..不完全确定我的编辑与文本正确匹配,似乎他们没有很好地显示。 – RicL