使用spring.net实现IOC

下载地址:spring.net下载地址

1.新建一个控制台程序springTest,引用dll。

Spring.NET > bin > net > 4.0 > release下找到 Comon.Logging.dll和Spring.Core.dll这两个dll复制到项目中添加引用

2.例子

2.1 定义接口和方法实现放在一起
   interface IPerson
    {
        void Show();
    }

   public class zhangsan : IPerson
    {
        public void Show()
        {
            Console.WriteLine("Hello World 张三");
        }
    }
    
    public class LiSi : IPerson
    {
        public string Name { get; set; }
        public void Show()
        {
            Console.WriteLine("Hello World 李四  属性注入" + Name);
        }
    }


   public  class refioc
    {
       public Test refTest { get; set; }
    }
    
    public class Test
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public zhangsan refTest11 { get; set; }
        public Test(string name, int age, zhangsan refTest1)
        {
            Name = name;
            Age = age;
            refTest11 = refTest1;
        }

        public void sendTest()
        {
            Console.WriteLine("I am SENDtest");
        }
    }
2.2.声明一个泛型ContextRegistry来操作容器
    public class ContextRegistry<T> where T : class
    {
        public static T GetIoc(string name)
        {
            //ContextRegistry已经是单例模式了
            //创建实例的方式转为容器帮我们创建
            //创建容器上下文
            IApplicationContext ctx = ContextRegistry.GetContext();
            T person = ctx.GetObject(name) as T;
            return person;
        }
    }
2.3.app.config配置:主要是 < configSections> 和两个节点里面的内容,< configSections>的内容是固定的,要修改的是< spring>下的内容
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <!-- 这个节点要紧靠在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>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <!-- spring.Net节点配置-->
  <spring>
    <context>
      <!-- 容器配置,将下面的配置放在xml中进行配置方便修改-->
      <resource uri="file://person.xml"/> 
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
      <!-- 所有的配置在person.xml-->
    </objects>
  </spring>
</configuration>

2.4.把容器单独写到一个xml文件

因为每个需要用到控制反转的类都要配置到配置文件中,如果非常多类,那配置文件就很难看了。所以最好把配置放到一个单独的xml文件中

2.4.1 添加一个xml文件这里命名为:person.xml放到一个lib文件夹里

2.4.2 把app.config配置文件中的 objects节点复制到person.xml文件中,然后把app.config配置文件中objects里面的节点都删除,注意objects节点保留

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <description>An  example that demonstrates simple IoC features.</description>
  <!-- 容器-->
  <!-- name要唯一,type="类的全名称,所在程序集"-->
  <object name="zhangsan" type="springnet.zhangsan,springnet">
    
  </object>
  <object name="LiSi" type="springnet.LiSi,springnet">
    <!--01属性注入-值类型-->
    <property name="Name" value="Linq"></property>
  </object>

  <object name="refioc" type="springnet.refioc,springnet">
    <!--01属性注入-引用类型-->
    <property name="refTest" ref="Test"></property>
  </object>
  
  <object name="Test" type="springnet.Test,springnet">
    <!--03构造函数注入-->
    <constructor-arg name="name"   value="config配置"></constructor-arg>
    <constructor-arg name="age" value="25"></constructor-arg>
    <constructor-arg name="refTest1" ref="zhangsan"></constructor-arg>
  </object>
</objects>
2.5. 在控制台程序中执行
static void Main(string[] args)  static void Main(string[] args)
        {
            //创建实例的方式转为容器帮我们创建
            //创建容器上下文
            IPerson dd = ContextRegistry<IPerson>.GetIoc("zhangsan");
            dd.Show();

            IPerson dd1 = ContextRegistry<IPerson>.GetIoc("LiSi");
            dd1.Show();
           
            Test dd111 = ContextRegistry<Test>.GetIoc("Test");

            refioc d = ContextRegistry<refioc>.GetIoc("refioc");


            Console.ReadKey();
        }
2.6.结果

在这里插入图片描述

参考文档:
https://www.cnblogs.com/lgxlsm/p/5410944.html
https://www.cnblogs.com/wei325/p/5406283.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值