spring.net 基本用法

首先明白两个概念
Ioc:控制翻转 就是创建对象的权利由开封人员自己控制New,转到了由容器来控制

DI: 依赖注入,就是通过容器来创建对象的时候,在对象初始化是可以给一些属性,构造方法的参数等注入默认值可以是复杂类型

而spring.net 本质就是在容器中解决对象的创建问题

首先要引入相关的DLL 文件
Spring.Core.dll ,Spring.Core.pdb ,Spring.Core.xml

Common.Logging.dll 这里需要引入这个程序集文件否则在创建容器的时候发会报错

Spring.net 就是在配置文件中进行相关设置

<configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
//其中spring分context和 object部分 ,创建对象的实例主要在 objects部分
 <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
    //这里的name名称可以随意起,   type部分逗号前为类的全名即命名空间+类名逗号后为程序集名称
      <object name="IuserInfoService" type="demo.UserInfoServices,demo"></object>
      
      <object name="person" type="demo.person,demo">
       //在object里面添加property 可为类中的属性赋值
        <property name="Name" value="郭海天"/>
        <property name="Age" value="34"/>
      </object>
      <object name="Student" type="demo.Student,demo">
      //此节点为给类中的构造函数参数赋值如赋值的化程序报错,
      index代表参数  索引从0开始
        <constructor-arg index="0" value="嘎嘎"/>
        <constructor-arg index="1" value="11"/>
      </object>
    </objects>
  </spring>


在程序中应用spring.net首先引入如下命名空间

using Spring.Context;
using Spring.Context.Support;
//得到程序集所在的容器
  IApplicationContext ctx1 = ContextRegistry.GetContext();
  //读取配置文件创建对象的实例
  IUserInfoService userInfo = (IUserInfoService)ctx1.GetObject("IuserInfoService");

mvc中如何使用spring.net

除了引用Spring.Core.dll ,Spring.Core.pdb ,Spring.Core.xml,Common.Logging.dll 还需要引用 Spring.Web.dll,
Spring.Web.Extensions.dll Spring.Web.Extensions.pdb Spring.Web.Extensions.xml Spring.Web.Mvc4.dll
Spring.Web.Mvc4.pdb Spring.Web.Mvc4.xml Spring.Web.pdb Spring.Web.xml
在UI层新建config文件夹,在建立 controllers.xml文件

在webconfig配置文件中配置

<configSections>

    <sectionGroup name="spring">
      <!--MVC中的spring.Net的配置-->
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <!--获取config文件夹下的controllers.xml配置文件-->
      <resource uri="file://~/Config/controllers.xml"/>
    
    </context>
  </spring>

controllers.xml 配置文件中配置如下

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
//这个object 创建UserInfoController控制器对象实例,type逗号前面为控制器全名称(命名空间+类名)逗号后面为程序级,因UserInfoController
在controller文件夹中,因此命名空间和程序集名称不一致   singleton=false 表示是否进行单例模式 这里选否
  <object type="OAUI.Controllers.UserInfoController, OAUI" singleton="false" >
  //在控制器下有个iuService属性  关联一个 UserInfoService 的复杂属性
  
    <property name="iuService" ref="UserInfoService" />
        <property name="..控制器中的属性名“ref="关联的object" />
  </object>
  //这个object 中 得到OABLL.UserInfoService  name名称为 UserInfoService 与上面的控制器进行关联 
  <object type="OABLL.UserInfoService,OABLL" singleton="false" name="UserInfoService" >
  </object>
  <object type="其他类的实例,其他程序集" singleton="false" name="..控制器中的属性名" >
  </object>

  
 
</objects>

最后在Global.asax 进行修改

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Optimization;
using System.Web.Routing;
using Spring.Context;
using Spring.Web.Mvc;
namespace OAUI
{
 
//这里的SpringMvcApplication 需引用 Spring.Web.Mvc;SpringMvcApplication 继承自System.Web.HttpApplication 
    public class MvcApplication :SpringMvcApplication//System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值