C# 用Spring.NET将依赖注入MVC控制器
控制器中的对象在运行时未被注入 Web.config:C# 用Spring.NET将依赖注入MVC控制器,c#,.net,asp.net-mvc,dependency-injection,spring.net,C#,.net,Asp.net Mvc,Dependency Injection,Spring.net,控制器中的对象在运行时未被注入 Web.config: <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> <section name="objects" type="Spring.Context.Support.DefaultSectionHandle
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
问题:
在运行时,ObjectA不会被注入并保持为null,这
在整个代码中导致空异常
备选方案:
我可以手动初始化Spring对象,并使用以下代码获取它的对象
var ctx = ContextRegistry.GetContext();
var objectA = ((IObjectFactory)ctx).GetObject("ObjectImpl") as ClassA;
事实证明,我遗漏了MVC的Spring实现的一个相当重要的部分 我的解决方案是添加一个DependencyResolver,它实现了IDependencyResolver 从属解析程序:
public class SpringDependencyResolver : IDependencyResolver
{
private readonly IApplicationContext _context;
public SpringDependencyResolver(IApplicationContext context)
{
_context = context;
}
public object GetService(Type serviceType)
{
var dictionary = _context.GetObjectsOfType(serviceType).GetEnumerator();
dictionary.MoveNext();
try
{
return dictionary.Value;
}
catch (InvalidOperationException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _context.GetObjectsOfType(serviceType).Cast<object>();
}
}
您可以实现自己的
idependencysolver
,就像您在回答中建议的那样。但是请注意,由于(iirc)版本1.3.1 Spring.NET内置了对asp.NET mvc和的支持。Spring.NET2.0()也内置了对版本4.0的支持。请考虑使用这些库。
您可能有兴趣将您的
SpringDependencyResolver
与之进行比较。您遗漏了一些难题。请看一下,还有,@nemesv是的,你是对的,原来我遗漏了一块拼图。谢谢谢谢@Marijn的建议。使用Spring.NET内置支持是正确的。但我们的问题是,我们使用的是MVC4框架,而Spring.NET2.0还没有准备好发布。因此,定制实现更适合我们的情况。我来看看DependencyResolver,谢谢!我试图使用Spring提供的依赖项解析程序,但在我的Global.asax(方法init,从mvcapapplication继承)“DependencyResolver.SetResolver(新的SpringDependencyResolver(ContextRegistry.GetContext());”中抛出错误,表示上下文仍在创建中。你知道我的情况出了什么问题吗?这行不通,因为上下文正在创建中,ContextRegistry.GetContext()
将引发异常!
public class TestController: Controller
{
// this object will be injected by Spring.net at run time
private ClassA ObjectA { get; set; }
var ctx = ContextRegistry.GetContext();
var objectA = ((IObjectFactory)ctx).GetObject("ObjectImpl") as ClassA;
public class SpringDependencyResolver : IDependencyResolver
{
private readonly IApplicationContext _context;
public SpringDependencyResolver(IApplicationContext context)
{
_context = context;
}
public object GetService(Type serviceType)
{
var dictionary = _context.GetObjectsOfType(serviceType).GetEnumerator();
dictionary.MoveNext();
try
{
return dictionary.Value;
}
catch (InvalidOperationException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _context.GetObjectsOfType(serviceType).Cast<object>();
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
DependencyResolver.SetResolver(new SpringDependencyResolver(ContextRegistry.GetContext()));
}