Spring.Net 简单入门学习

Spring.NET IoC容器的用法。

通过简单的例子学习Spring.Net

1、先创建一个控制台程序项目。

2、添加IUserInfoDal 接口。

namespace Spring.Net
{
    public interface IUserInfoDal
    {
        void Show();
    }
}

3、添加AdoNetUserInfoDal类和EFUserInfoDal类,继承IUserInfoDal接口。

AdoNetUserInfoDal.cs

public class AdoNetUserInfoDal:IUserInfoDal
    {
        public void Show()
        {
            Console.WriteLine("我是 AdoNet Dal );
        }
    }

EFUserInfoDal.cs

public class EFUserInfoDal: IUserInfoDal
    {
        public void Show()
        {
            Console.WriteLine("我是EF Dal);
        }     
    }

4、引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll

5、添加Spring.Net配置节点,配置object节点

"1.0" encoding="utf-8" ?>

  
  
    
    "spring">
      
"context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
"objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> "v4.0" sku=".NETFramework,Version=v4.5" /> "config://spring/objects"/> "http://www.springframework.net"> An example that demonstrates simple IoC features. <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"> object>

6、开始写主函数,创建spring容器上下文

namespace Spring.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            //控制权没有反转
            //IUserInfoDal infoDal = new EFUserInfoDal();

            //Spring.Net 创建实例的方式转为容器帮我们创建
            //创建spring容器上下文
            IApplicationContext ctx = ContextRegistry.GetContext();
            //通过容器创建对象
            IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
            efDal.Show();
            Console.ReadKey();
        }
    }
}

Spring.Net 简单入门学习_第1张图片

 

7、属性注入


  
    
      
      "config://spring/objects"/>
    
    "http://www.springframework.net">
      
      An  example that demonstrates simple IoC features.
      
      <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net">
        "Name" value="张三"/>
        
        "UserInfo" ref="UserInfo"/>
      object>      

      
      <object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net">
        "Name" value="李四"/>
        "Age" value="15"/>
      object>
    
  

8、构造函数注入


<object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net">
   "0" value="张三"/>
   "1" ref="UserInfo"/>
object>

9、容器配置


   
   "config://spring/objects"/>
   
   
   
   

10、完整例子

IUserInfoDal.cs

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

namespace Spring.Net
{
    public interface IUserInfoDal
    {
        UserInfo UserInfo { get; set; }
        string Name { get; set; }

        void Show();
    }
}

AdoNetUserInfoDal.cs

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

namespace Spring.Net
{
    public class AdoNetUserInfoDal:IUserInfoDal
    {
        public AdoNetUserInfoDal(string name, UserInfo userInfo)
        {
            Name = name;
            UserInfo = userInfo;
        }
        public UserInfo UserInfo { get; set; }
        public string Name { get; set; }

        public void Show()
        {
            Console.WriteLine("我是 AdoNet Dal ,属性注入:Name=" + Name);
            Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age);
        }
    }
}

EFUserInfoDal.cs

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

namespace Spring.Net
{
    public class EFUserInfoDal: IUserInfoDal
    {
        public EFUserInfoDal()
        {

        }
        public UserInfo UserInfo { get; set; }
        public string Name { get; set; }

        public void Show()
        {
            Console.WriteLine("我是EF Dal,属性注入:Name=" + Name);
            Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age);
        }     
    }
}

App.config

"1.0" encoding="utf-8" ?>

  
  
    
    "spring">
      
"context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
"objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> "v4.0" sku=".NETFramework,Version=v4.5" /> "config://spring/objects"/> "http://www.springframework.net"> An example that demonstrates simple IoC features. <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"> "Name" value="张三"/> "UserInfo" ref="UserInfo"/> object> <object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net"> "0" value="张三"/> "1" ref="UserInfo"/> object> <object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net"> "Name" value="李四"/> "Age" value="15"/> object>

Program.cs

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

//Spring.Aop.dll 面向切面编程
//Spring.Core.dll spring框架基础
//Common.Logging.dll 这个必须也要引用

namespace Spring.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            //控制权没有反转
            //IUserInfoDal infoDal = new EFUserInfoDal();

            //Spring.Net 创建实例的方式转为容器帮我们创建
            //第一步,引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll
            //第二步,添加Spring.Net配置节点
            //第三步,配置object节点
            //第四步,创建spring容器上下文
            IApplicationContext ctx = ContextRegistry.GetContext();
            //第五步,通过容器创建对象
            IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
            efDal.Show();

            IUserInfoDal adoDal = ctx.GetObject("UserInfoDal2") as IUserInfoDal;
            adoDal.Show();

            Console.ReadKey();
        }
    }
}

Spring.Net 简单入门学习_第2张图片

下面是MVC项目中使用Spring.Net融合的例子

1、将Spring.net程序集复制到项目下的packages文件夹中。

2、项目中添加引用Spring.Core.dll、Common.Logging.dll、Spring.Web.Mvc4.dll、Spring.Web.dll

3、修改Web.config,配置节点


  
    
    
"entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> "spring">
"context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/> "file://~/Config/controllers.xml"/>

4、项目中创建Config文件夹,controllers.xml文件。

"1.0" encoding="utf-8" ?>
"http://www.springframework.net">

  <object type="HX.Shop.UI.Main.Controllers.AdminController, HX.Shop.UI.Main" singleton="false" >
    "AdminService" ref="AdminService" />
  object>
  <object name="AdminService" type="HX.Shop.BLL.AdminService, HX.Shop.BLL" singleton="false" >
    
  object>

5、Global.asax 中修改 SpringMvcApplication

public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication //System.Web.HttpApplication

 

摘自:http://www.cnblogs.com/han1982/p/4177850.html

自己学习的同时,写了个例子,通过360免费云盘进行分享
下载链接:https://yunpan.cn/cYWTKQcZUPnxn (提取码:880a)

开发环境:VS2013

 

你可能感兴趣的:(Spring.Net 简单入门学习)