星空网 > 软件开发 > ASP.net

SpringNet学习笔记一

---恢复内容开始---

    最近看了园子里的大神分享的springnet框架的知识,感觉挺不错的,自己闲下来也研究研究springnet。这几天看了springnet容器的基础篇IOC和AOP,也有点个人理解分享一下,记录一下自己的学习笔记,欢迎大家提出问题一块研究解决。

    入门springnet首先了解一下什么是控制反转,首先控制反转(Inversion of Control,英文缩写为IoC),也叫依赖注入(Dependency Injection)。      个人理解springnet的控制反转及其像一个复杂的抽象工厂。控制反转就是将原来由开发人员来创建实例的过程转换为容器注入完成加载实例,最终实现松耦合。这点和利用抽象工厂来反射动态加载程序集完成实例的创建很相似。纯属个人理解,不足的地方望纠正一下。叙述了控制反转的用途,下面来说一下操作。

    总结了一下实现springnet依赖注入的步骤:

  1. 添加程序集的引用
  2. 添加解析节点
  3. 添加spring配置节点和object配置节点
  4. 创建容器上下文

   大致可分为上5点,不足的地方望纠正,下面写点代码演示一下我的依赖注入实现以及和抽象工厂的差别:

    首先新建一个接口IPerson,接口内只提供了Person的方法签名

namespace Myspringdemo{  public interface IPerson  {    void Person();  }}

在新建一个NewPreson类来继承IPerson实现接口中的方法

SpringNet学习笔记一SpringNet学习笔记一
namespace Myspringdemo{  public class NewPerson : IPerson  {    public void Person()    {      Console.WriteLine("我是工厂造的人");    }  }}

View Code

 

 

 为了与抽象工厂做对比,我又建了一个工厂类实现创建实例

SpringNet学习笔记一SpringNet学习笔记一
namespace Myspringdemo{  public class GetFactory  {    public static IPerson person;    public static IPerson Instance()    {      person = CallContext.GetData("Person") as IPerson;      if (person == null)      {        person = new NewPerson();        CallContext.SetData("Person", person);      }      return person;    }  }}

GetFactory

然后我们在program类中调用一下

namespace Myspringdemo{  public class Program  {    static void Main(string[] args)    {      IPerson Factoryper = GetFactory.Instance();      Factoryper.Person();//工厂实现方法    }  }}

结果如下

SpringNet学习笔记一

以上是抽象工厂实现,下面介绍一下依赖注入的实现

1.引用程序集:Common.Logging.dll和spring.core.dll

2.配置解析节点

 <!--配置spring节点解析类型--> <!--configSections节点必须紧跟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>

3.配置spring节点和objects节点,并配置object节点简单的依赖注入完毕

<!--配置spring.net节点--> <spring>  <context>   <resource uri="config://spring/objects"/>  </context>  <objects "http://www.springframework.net">      <description>描述文档</description>   <!--放置容器里的节点位置-->   <!--type为将要反射的程序集,写法为当前命名空间名.类名,命名空间名-->   <object name="Myspring"   type="Myspringdemo.SpringPerson,Myspringdemo">  </object>     </objects> </spring>

OK节点配置完毕,下面就是调用了,新建一个springperson类来继承IPerson,并实现接口中的方法

namespace Myspringdemo{  public class SpringPerson:IPerson  {    public void Person()    {      Console.WriteLine("我是spring造的人"); ;    }  }}

实现依赖注入的方法

namespace Myspringdemo{  public class Program  {    static void Main(string[] args)    {      IApplicationContext ctx = ContextRegistry.GetContext();//创建上下文      IPerson per = ctx.GetObject("Myspring") as IPerson;      per.Person();//依赖注入实现方法调用      IPerson Factoryper = GetFactory.Instance();      Factoryper.Person();//工厂实现方法    }  }}

让我们来打印一下看看结果

SpringNet学习笔记一

    成功打印了依赖注入的结果,一开始接触依赖注入感觉和工厂没有太大区别,搞了几天后,发现依赖注入的方法确实比工厂可维护性和可扩展性要好一点,效率方面可能也要好一点,这个我没有做多了解,好心的园友可以纠正一下,谈谈不一样的理解!

    

---恢复内容结束---




原标题:SpringNet学习笔记一

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

QA优化:https://www.goluckyvip.com/tag/7294.html
境外债开户:https://www.goluckyvip.com/tag/72940.html
国外 外汇开户:https://www.goluckyvip.com/tag/72941.html
外国开户:https://www.goluckyvip.com/tag/72943.html
国外开户:https://www.goluckyvip.com/tag/72947.html
境外 开户:https://www.goluckyvip.com/tag/72948.html
三百元以内千兆路由器怎么选择?:https://www.vstour.cn/a/363184.html
千岛湖绿城度假酒店的简介:https://www.vstour.cn/a/363185.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流