• Spring.NET学习笔记(1)基本依赖注入


         不学Spring,系统照样能跑,学会了Spring会发现跟没学会之前也多大差别,一直在用Spring以后就会感觉不用Spring写程序就好像无从下手,Spring就是一容器,用以组装程序而用。

    一.依赖注入

    分三种方式

    (1)构造注入

    <object id="foo" type="X.Y.Foo, Example">
      <constructor-arg ref="bar"/>
      <constructor-arg ref="baz"/>
    </object>
    
    <object id="bar" type="X.Y.Bar, Example"/>
    <object id="baz" type="X.Y.Baz, Example"/>


    (2)Setter属性注入

    <object id="exampleObject" type="Examples.ExampleObject, ExamplesLibrary">
    
      <!-- setter injection using the ref attribute -->
      <property name="objectOne" ref="anotherExampleObject"/>
      <property name="objectTwo" ref="yetAnotherObject"/>
      <property name="IntegerProperty" value="1"/>
    </object>
    
    <object id="anotherExampleObject" type="Examples.AnotherObject, ExamplesLibrary"/>
    <object id="yetAnotherObject" type="Examples.YetAnotherObject, ExamplesLibrary"/>


    (3)方法注入

    3.1

    public class TestObjectFactory
    {
        public const string TheName = "Old Goriot";
        public const int TheAge = 78;
    
        public virtual object GetObject()
        {
            return new TestObject(TheName, TheAge);
        }
    }
    <object id='factory' type='Spring.Objects.Factory.Support.TestObjectFactory, Spring.Core.Tests'>
      <lookup-method name='GetObject' object='target'/>
    </object>
    <object id='target' type='Spring.Objects.TestObject, Spring.Core.Tests' singleton='false'>
      <property name='name' value='Fiona Apple'/>
      <property name='age' value='47'/>
    </object>


    3.2方法替换

    public class MyValueCalculator
    {
    
        public virtual string ComputeValue(string input)
        {
            // ... some real code
        }
    
        // ... some other methods
    }
    public class ReplacementComputeValue : Spring.Objects.Factory.Support.IMethodReplacer
    {
        public object Implement(object target, MethodInfo method, object[] arguments)
        {
            // get the input value, work with it, and return a computed result...
            string value = (string)arguments[0];
            // compute...
            return result;
        }
    }

    MyValueCalculator 的ComputeValue方法替换成,实现IMethodReplacer 接口的Implement方法

    <object id="myValueCalculator" type="Examples.MyValueCalculator, ExampleAssembly">
      <!-- arbitrary method replacement -->
      <replaced-method name="ComputeValue" replacer="replacementComputeValue">
        <arg-type match="String"/>
      </replaced-method>
    </object>
    
    <object id="replacementComputeValue" type="Examples.ReplacementComputeValue, ExampleAssembly"/>

    这个方法明显很变态,完全破坏了封装,一般很少用
  • 相关阅读:
    Mac Sublime Text 3 配置Python环境及安装插件
    iOS 简单易用的二维码扫描及生成二维码三方控件LFQRCode,可灵活自定义UI
    iOS 自定义相机带拍摄区域边框及半透明遮罩层(含源码)
    Xcode8 不能显示blame,show blame for line 灰色不可点解决办法
    github + SourceTree管理自己的库并上传到cocoapods及各种坑的解决办法
    Mysql查询的一些操作(查表名,查字段名,查当月,查一周,查当天)
    SqlServer表结构查询
    NET(C#):XmlArrayItem特性和XmlElement特性在序列化数组的差别
    Git常用命令
    MySQL添加用户、删除用户与授权
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1553474.html
Copyright © 2020-2023  润新知