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 ...
随机推荐
- uva-185-暴力枚举
请相信,这是一道水题,读了一周的题意 题意: 题目里面描述的那三个条件可以直接无视,关于罗马数字只要知道一个规则即可,映射如下 I 1 V 5X 10 L 50C 100 D 50 ...
- 本地git仓库常用操作
SSH配置: 本机创建SSH key $ ssh-keygen -t rsa -C "youremail@example.com" 将SSHkey添加到git仓库:id_rsa.p ...
- 0_Simple__simpleVoteIntrinsics + 0_Simple__simpleVoteIntrinsics_nvrtc
介绍了线程束表决函数的实例(其概念介绍见 http://www.cnblogs.com/cuancuancuanhao/p/7841512.html),并在静态和运行时编译两种条件下进行使用. ▶ 源 ...
- hive 锁
HiveQL是一种SQL语言,但缺少udpate和insert类型操作时的行,列或者查询级别的锁支持,hadoop文件通常是一次写入(支持有限的文件追加功能),hadoop和hive都是多用户系统,锁 ...
- HTML5 Canvas ( 圆和切点曲线的绘制 ) arc, arcTo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java 调用apache.commons.codec的包简单实现MD5加密
转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...
- Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match
spring 4.0.2,mybatis 3.2.6,aspectjweaver 1.8.10 使用的时候,报错: Caused by: java.lang.IllegalStateException ...
- as3 单例的不常见写法
方法一:(显式允许new一次) package { import flash.errors.IllegalOperationError; import flash.events.EventDispat ...
- as2 针对加载进来的swf操作
如果加载的子swf,里面的东西会随时发生变化,那么as2获取的子swf宽高也会不停在变动. 解决方法,就是在子swf里面控制设置方法,当as2需要准确获取子swf宽高时,迫使子宽高恢复到原本样子,这样 ...
- 多线程--Java
多线程: 1.进程和线程 进程是资源分配的最小单位,线程是CPU调度的最小单位. 每个进程的创建都需要系统为其开辟资源内存空间,并发执行的程序在执行过程中分配和管理资源的基本单位,速度和销毁也较慢.进 ...