Spring.net之Asp.net mvc配置

本文介绍了如何使用Spring.NET框架进行依赖注入(IOC)容器的配置。通过创建接口和实现类,配置web.config文件以及IocService.xml来实现对象的管理和依赖注入。最终实现了通过ServiceFactory调用对象的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


注:MvcApplication类继承由原来的HttpAccpication变为SpringMvcApplication类。SpringMvcApplication继承自HttpAccpication。
容器会在请求开始时创建配置好的对象。
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容器配置。









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值