Spring.NET学习笔记(1)-基本依赖注入
来源:互联网 发布:华为网盘mac版 编辑:程序博客网 时间:2024/04/29 09:11
不学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"/>
这个方法明显很变态,完全破坏了封装,一般很少用
- Spring.NET学习笔记(1)-基本依赖注入
- Spring.NET学习笔记(1)-基本依赖注入
- spring.net学习笔记1--依赖对象注入
- Spring学习笔记(1)---依赖注入
- Spring 学习笔记 ----依赖注入
- [学习笔记]Spring依赖注入
- Spring学习笔记 依赖注入
- 【Spring学习笔记】依赖注入
- Spring.NET学习笔记(2)-依赖注入细节
- Spring.NET学习笔记(2)-依赖注入细节
- ASP.NET_MVC使用Spring.Net.MVC依赖注入学习笔记
- spring学习笔记3--依赖注入对象
- Spring 学习笔记 - IOC/依赖注入
- Spring学习笔记(8)-----------依赖注入
- spring学习笔记(4)依赖注入详解
- spring 依赖注入DI-个人学习笔记
- 【Spring学习笔记二】-理解依赖注入
- spring学习笔记 依赖注入详解
- OpenGL入门学习(四)
- 最近很不务正业
- 选择排序法
- OpenGL入门学习(五)
- 基于数据挖掘技术入侵检测系统研究
- Spring.NET学习笔记(1)-基本依赖注入
- OpenGL入门学习(六)
- 晚会后记之一
- [推荐] 面试英语技巧
- Spring.NET学习笔记(2)-依赖注入细节
- Spring.NET学习笔记(3)-注册事件注入
- Spring.NET学习笔记(4)-对象作用域和类型转换
- SVN的安装以及配置
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之36---支持BREW的手机