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 ...
随机推荐
- Sklearn与特征工程
Scikit-learn与特征工程 “数据决定了机器学习的上限,而算法只是尽可能逼近这个上限”,这句话很好的阐述了数据在机器学习中的重要性.大部分直接拿过来的数据都是特征不明显的.没有经过处理的或者说 ...
- HTML5 Canvas ( 文字横纵对齐 ) textAlign, textBaseLine
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- shiro 没有权限异常处理
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> ...
- jmeter java请求:java.lang.VerifyError: Cannot inherit from final class
被这个问题block了一天,应该是包冲突的.通过对包删减排查,结果发现是netty-all-xxx.Final.jar包的问题 应该是jmeter版本和netty版本的冲突吧,换成jmeter 3.1 ...
- Producer-consumer problem in Python
from: http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/ By : Akshar Raaj We will s ...
- Flume环境搭建_五种案例(转)
Flume环境搭建_五种案例 http://flume.apache.org/FlumeUserGuide.html A simple example Here, we give an example ...
- JAVA学习(七)__Spring的@Autowired注入规则
@Autowired 默认是按照byType进行注入的,但是当byType方式找到了多个符合的bean,又是怎么处理的? 经过一些代码的测试,我发现,Autowired默认先按byType,如果发现找 ...
- Redis进阶实践之一VMWare Pro虚拟机安装和Linux系统的安装
一.引言 设计模式写完了,相当于重新学了一遍,每次学习都会有不同的感受,对设计模式的理解又加深了,理解的更加透彻了.还差一篇关于设计模式的总结的文章了,写完这篇总结性的文章,设计模式的文章就暂时要告一 ...
- CGBitmapContextCreate函数参数详解 以及在 ios7下变化
函数原型: CGContextRef CGBitmapContextCreate ( void *data, size_t width, size_t height, size_t ...
- CentOS7使用yum详细搭建zabbix3.2过程
本文引用于:http://blog.csdn.net/s3275492383/article/details/62417210 一.准备搭建环境: 1.系统:CentOS7 2.默认有使用linux服 ...