zoukankan      html  css  js  c++  java
  • Spring.Net 简单实例-01(IOC)

    1.话不多说看操作.新建"Windows窗体应用程序"

    2:通过配置文件创建IOC容器

      首先引入安装包

    3:定义一个接口(更好的体现封装性,当然也可以直接使用类)

      定义一个类,实现接口

     4:配置App.config文件

    <?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>
          <!--//type属性值必须是包含程序集名称在内的类型全名    "命名空间,程序集"-->
          <object name="UserInfoService"  type="YK.OA.SpringNet.UserInfoService, YK.OA.SpringNet">
            
            </object>
        </objects>
      </spring>
    </configuration>
    App.config

     5:添加测试按钮

    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 YK.OA.SpringNet
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                IApplicationContext ctx = ContextRegistry.GetContext();
                //GetObject获得app配置文件的objext项=>创建实例
                IUserInfoService lister = (IUserInfoService)ctx.GetObject("UserInfoService");
                MessageBox.Show(lister.ShowMsg());
            }
        }
    }
    按钮代码(注意命名空间)

    6:运行

  • 相关阅读:
    事务
    触发器
    入行大数据必须知道的事!
    5G如何使云计算更加前卫
    2021年加密货币和区块链风向
    2020年数据存储管理发生的7种变化
    如何克服物联网中数据集成的挑战
    从开发到产出:关于机器学习的七则干货建议
    如何利用机器学习进行静态分析
    AI如何改变DevOps?
  • 原文地址:https://www.cnblogs.com/YK2012/p/6597024.html
Copyright © 2011-2022 走看看