Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200
1.person类
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person Friend { get; set; }
}
2. 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));
}
}
3.配置文件
<?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>
4.调用
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
PersonDao dao = ctx.GetObject("personDao") as PersonDao;
dao.Get();
Console.ReadLine();
}
}
5.结果
Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200的更多相关文章
- Spring.NET学习笔记8——集合类型的注入(基础篇)
1.基础类 public class Happy { public override string ToString() { return &q ...
- Spring.NET学习笔记6——依赖注入(应用篇)
1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...
- Spring.Net学习笔记(2)-依赖注入
一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...
- Spring MVC学习笔记——返回JSON对象
1.想要GET请求返回JSON对象,首先需要导入jackson-all-1.9.4.jar包 2.在控制器中添加不同的show()方法 //show()方法返回JSON对象 @RequestMappi ...
- Spring.Net学习笔记(4)-属性及构造器注入
一.开发环境 操作系统:Win10 编译器:VS2013 .Net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll:1.3.1 Common.Logging.d ...
- python学习笔记六 初识面向对象上(基础篇)
python面向对象 面向对象编程(Object-Oriented Programming )介绍 对于编程语言的初学者来讲,OOP不是一个很容易理解的编程方式,虽然大家都知道OOP的三大特性 ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring学习笔记之依赖的注解(2)
Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...
随机推荐
- django的小操作,查询效率up, 引用art-template模板+djangorestframework
Part1: 提高查询效率newses = News.objects.select_related('category', 'author').get(id=1) # category和author字 ...
- 逆地址解析协议RARP
解决的问题 一般系统启动时,从引导磁盘中获取ip 有些机器没有引导磁盘,如X终端或无盘工作站,则需要采用其他方法来获得IP地址 解决的过程 无盘系统依据RARP协议 从接口卡上读取唯一的硬件地址,然后 ...
- 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)
转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...
- Astah professional 7.2
分享Astah professional 7.2下载和破解: 官方下载地址:http://astah.net/download 1.免费的community版本 链接:http://pan.baidu ...
- PL/SQL Developer安装教程以及汉化包安装教程
一.安装PL/SQL 1.百度下载plsql破解版软件,官网只能使用30天 2.双击plsqldev906.exe进行安装,点击 iagree 3.默认是安装在c盘,可以根据自己需要更改安装目录 4. ...
- eclipse使用ctrl+shift+F格式化代码失效
通常情况出现这种问题是组合快捷键和别的软件快捷键冲突了, 最常见的是和搜狗输入法冲突, 在设置中找到搜狗输入法然后把冲突的快捷键取消掉就可以了.
- jquery 获取和设置Select选项常用方法总结
1.获取select 选中的 text:$("#cusChildTypeId").find("option:selected").text();$(" ...
- list接口如何使用
1集合类,在java语言中的java.util包提供了一些集合类,这些集合类又被称作容器. 2区别集合类和数组.(1)数组的长度是固定的,集合的长度是可变的.(2)数组是用来存放基本数据类型的,集合是 ...
- JSP技术复习
JSP是一种运行在服务器端的脚本语言,是用来开发动态网页的技术,它是Java Web程序的开发重要技术 JSP页面主要由HTML和JSP代码构成,JSP代码是通过"<%"和& ...
- dp-最长公共子序列
最长公共子序列(NYOJ36) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公 ...