Srping.Net是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Srping.Net以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。
创建需要用容器创建的对象:
public interface IPerson
{
string Eat();
}
public interface IStudent
{
string GoSchool();
}
public class Person:IPerson
{
public string Name { get; set; }
public override string ToString()
{
return "this is a person";
}
public string Eat()
{
return "i can eat";
}
}
public class Student : Person, IStudent
{
public string School { get; set; }
public override string ToString()
{
return "this is a student";
}
public string GoSchool()
{
return "i need to school";
}
}
创建服务工厂
public class ServiceFactory
{
//注:此处Student,Person属性必须和类名称一样,因为默认自动装配是根据类名类装配的
public static IStudent Student { get; set; }
public static IPerson Person { get; set; }
}
创建mvc项目
web.config配置
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
<resource uri="assembly://MvcApplication1/MvcApplication1.config/IocService.xml"></resource>
</context>
<objects xmlns="http://www.springframework.net" />
</spring>
IocService.xml配置
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd" default-autowire="byName">
<object id="Student" type="Entity.Student,Entity"></object>
<object id="Person" type="Entity.Person,Entity"></object>
<!--3.该对象需要放在最后-->
<object id="ServiceFactory" type="Service.ServiceFactory,Service" singleton="true"/>
</objects>
Global
容器会在请求开始时创建配置好的对象。
public abstract class SpringMvcApplication : HttpApplication
{
protected SpringMvcApplication()
{
}
protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
System.Web.Mvc.IDependencyResolver resolver = this.BuildDependencyResolver();
this.RegisterDependencyResolver(resolver);
System.Web.Http.Dependencies.IDependencyResolver resolver2 = this.BuildWebApiDependencyResolver();
this.RegisterDependencyResolver(resolver2);
}
protected virtual System.Web.Mvc.IDependencyResolver BuildDependencyResolver()
{
return new SpringMvcDependencyResolver(ContextRegistry.GetContext());
}
protected virtual System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver()
{
return new SpringWebApiDependencyResolver(ContextRegistry.GetContext());
}
protected virtual void ConfigureApplicationContext()
{
}
public override void Init()
{
base.Init();
this.ConfigureApplicationContext();
}
public virtual void RegisterDependencyResolver(System.Web.Http.Dependencies.IDependencyResolver resolver)
{
ThreadSafeDependencyResolverRegistrar.Register(resolver);
}
public virtual void RegisterDependencyResolver(System.Web.Mvc.IDependencyResolver resolver)
{
ThreadSafeDependencyResolverRegistrar.Register(resolver);
}
protected static class ThreadSafeDependencyResolverRegistrar
{
private static bool _isMvcResolverRegistered = false;
private static bool _isWebApiResolverRegistered = false;
private static readonly object Lock = new object();
public static void Register(System.Web.Http.Dependencies.IDependencyResolver resolver)
{
if (!_isWebApiResolverRegistered)
{
lock (Lock)
{
if (!_isWebApiResolverRegistered)
{
GlobalConfiguration.Configuration.DependencyResolver = resolver;
_isWebApiResolverRegistered = true;
}
}
}
}
public static void Register(System.Web.Mvc.IDependencyResolver resolver)
{
if (!_isMvcResolverRegistered)
{
lock (Lock)
{
if (!_isMvcResolverRegistered)
{
DependencyResolver.SetResolver(resolver);
_isMvcResolverRegistered = true;
}
}
}
}
}
}
调用
public ActionResult Index()
{
string test = ServiceFactory.Person.Eat();
return View();
}
这样就完成了spring.net的ioc容器配置。