Spring.NET学习笔记7——依赖对象的注入(基础篇)
一、属性注入
上篇我们简单提到依赖注入的用途。回顾一下所讲内容,发现在object节点下使用了<property name="Tool" ref="computer"/>。而property 标签正是用来属性注入的。而ref是用来标识是关联到哪个object。而name属性是指属性名。如下:
- <object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
- <property name="Tool" ref="computer"/>
- </object>
值类型的注入是需要使用property 节点的value属性。如<property name="Name" value="Liu Dong"/>
作为内联类型可以使用如下:
- <property name="Friend">
- <object type="SpringNetDi.Person, SpringNetDi"/>
- </property>
同理,内联类型可以是循环引用的对象(见代码处)。
二、构造函数注入
构造器注入使用constructor-arg标签作为标识。同样具有于属性注入相同的方式,使用name、ref、value作为构造器注入的属性,如下:
- <constructor-arg name="argPerson" ref="person"/>
- <constructor-arg name="intProp" value="1"/>
程序的代码如下:
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public Person Friend { get; set; }
- }
PersonDao
- public class PersonDao
- {
- private Person argPerson;
- private int intProp;
- public PersonDao(Person argPerson, int intProp)
- {
- this.argPerson = argPerson;
- this.intProp = intProp;
- }
- public void Get()
- {
- //构造函数注入的整型参数
- Console.WriteLine(string.Format("intProp:{0}", intProp));
- //构造函数注入的Person
- Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name));
- Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age));
- //内联对象Friend
- Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name));
- Console.WriteLine(string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age));
- //内联对象的循环引用
- Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name));
- Console.WriteLine(string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age));
- }
- }
App.config
- <?xml version="1.0" encoding="utf-8" ?>
- <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>
- <spring>
- <context>
- <resource uri="config://spring/objects" />
- </context>
- <objects xmlns="http://www.springframework.net">
- <object id="person" type="SpringNetDi.Person, SpringNetDi">
- <!--属性值类型注入-->
- <property name="Name" value="Liu Dong"/>
- <property name="Age" value="27"/>
- <!--内联对象注入-->
- <property name="Friend">
- <object type="SpringNetDi.Person, SpringNetDi">
- <property name="Name" value="Beggar"/>
- <property name="Age" value="23"/>
- <property name="Friend" ref="person"/>
- </object>
- </property>
- </object>
- <object id="personDao" type="SpringNetDi.PersonDao, SpringNetDi">
- <!--构造器注入-->
- <constructor-arg name="argPerson" ref="person"/>
- <constructor-arg name="intProp" value="1"/>
- </object>
- </objects>
- </spring>
- </configuration>
Program
- class Program
- {
- static void Main(string[] args)
- {
- IApplicationContext ctx = ContextRegistry.GetContext();
- PersonDao dao = ctx.GetObject("personDao") as PersonDao;
- dao.Get();
- Console.ReadLine();
- }
- }
输出效果如下:
分类:
orm技术
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2008-03-26 c# 二分查找
2008-03-26 几种字符串反转方法效率比较
2008-03-26 C#反转字符串效率最高的方法