Spring.Net-IOC和DI的实现

1.首先建一个Winform程序,点击引用,管理Nuget程序包,搜素Spring.Core,下载下来,实际上会添加Spring.Core和Common.Logging和Common.Logging.Core三个引用

2.将app.config文件替换为以下内容,从而使Spring.NET从标准的.NET配置文件中初始化IApplicationContext...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <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>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
      <!--将要创建的对象-->
      <object name="UserInfoService" type=" DXM.OA.SpringNet.UserInfoService,  DXM.OA.SpringNet">
        <property name="UserName" value="zhangsan"/>
        <!--引用下面的Person对象-->
        <property name="Person" ref="Person"/>
      </object>

      <!--将创建的Person对象-->
      <object name="Person" type=" DXM.OA.SpringNet.Person,  DXM.OA.SpringNet">
        <property name="Age" value="15"/>
      </object>
    </objects>

  </spring>
</configuration>

3.在配置文件中,通过<objects/>节点的<object/>子节点来配置程序中用到的对象。

4.在窗口按钮的单击事件中加入以下代码:

 IApplicationContext ctx = ContextRegistry.GetContext();
IUserInfoService service = (IUserInfoService)ctx.GetObject("UserInfoService");//声明接口的好处,可以和类解耦
MessageBox.Show(service.ShowMsg());

/*****************************************************************************************/

using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CZBK.ItcastOA.SpringNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            IApplicationContext ctx = ContextRegistry.GetContext();
            IUserInfoService lister = (IUserInfoService)ctx.GetObject("UserInfoService");
            MessageBox.Show(lister.ShowMsg());
        }
    }
}
 

/**************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXM.OA.SpringNet
{
    public interface IUserInfoService
    {
        string ShowMsg();
    }
}
/********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXM.OA.SpringNet
{
    public class Person
    {
        public int Age { get; set; }
    }
}
 

/********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXM.OA.SpringNet
{
    public class UserInfoService : IUserInfoService
    {
        public string UserName { get; set; }

        public Person Person { get; set; }

        public string ShowMsg()
        {
            return "Hello,World "+ UserName+":Age:"+Person.Age.ToString();
        }
    }
}

以上已经可以正常运行了

也可以将Objects抽取出来放到一个xml文件中,然后再app.config的context下面引用该xml文件,此时注意的是设置该xml文件的编译属性为始终复制,因为Spring.net只在bin/debug下面找文件,但app.config里面要保留objects节:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <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>
      <resource uri="config://spring/objects"/>
      <resource uri="file://services.xml"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
      <!--将要创建的对象--><!--
      <object name="UserInfoService" type=" DXM.OA.SpringNet.UserInfoService,  DXM.OA.SpringNet">
        <property name="UserName" value="zhangsan"/>
        --><!--引用下面的Person对象--><!--
        <property name="Person" ref="Person"/>
      </object>
      --><!--将创建的Person对象--><!--

      <object name="Person" type=" DXM.OA.SpringNet.Person,  DXM.OA.SpringNet">
        <property name="Age" value="15"/>
      </object>-->
    </objects>
  </spring>
</configuration>

/*******************************services.xml*********************************************************/

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <description>An  example that demonstrates simple IoC features.</description>
  <!--将要创建的对象-->
  <object name="UserInfoService" type=" DXM.OA.SpringNet.UserInfoService,  DXM.OA.SpringNet">
    <property name="UserName" value="zhangsan"/>
    <!--引用下面的Person对象-->
    <property name="Person" ref="Person"/>
  </object>
  <!--将创建的Person对象-->
  <object name="Person" type=" DXM.OA.SpringNet.Person,  DXM.OA.SpringNet">
    <property name="Age" value="15"/>
  </object>
</objects>
 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值