Spring.Net 技术简介 IOC and DI
一 简单介绍
二 创建对象,以及设置属性
创建好了项目,然后我们就要开始了<? xml version =" 1.0 " encoding =" utf-8 " ?>
< configuration>
< configSections>
< sectionGroup name = "spring " >
<!-- 这下面有context和object在下面都有对象的标签 -->
< section name = "context " type = "Spring.Context.Support.ContextHandler, Spring.Core " />
< section name = "objects " type = "Spring.Context.Support.DefaultSectionHandler, Spring.Core " />
</ sectionGroup >
</ configSections> < spring>
<!-- Spring.Net对象容器的配置 -->
< context >
<!-- 容器的所有的对象在哪里,这里用uri说明地址 -->
< resource uri = "config://spring/objects " />
<!-- 可以使用外部xml文件 --> </ context > <!-- objects:配置的容器里面的对象 -->
< objects xmlns = "http://www.springframework.net " >
< description >An example that demonstrates simple IoC features. </ description>
<!-- name最好用类名,type第一个是类的全称加上程序集,后面一个是项目名称 -->
< object name = "UserInfoDal " type = "SpringNetDemo.UserInfoDal, SpringNetDemo " >
<!-- 在这里是设置对象的属性,将Name的值设置成ctt -->
< property name = "Name " value = "ctt " />
</ object >
</ objects > </ spring> </ configuration>
public class UserInfoDal : IUserInfoDal
{
public string Name { get; set; } public void Show()
{
Console .WriteLine("zjh and "+Name );
}
}
class Program
{
static void Main(string [] args)
{
//IApplicationContext是Spring里面的一个超类,主要操作类
IApplicationContext ctx = ContextRegistry .GetContext();
//GetObject从配置文件中读取信息后,反射产生相应的对象,用as强转成对象的接口
IUserInfoDal userInfoDal = ctx.GetObject("UserInfoDal" ) as IUserInfoDal;
//轻松加愉快,就这样出来了
userInfoDal.Show();
Console .ReadKey();
}
}
三 创建复杂属性的设置
public class UserInfoService
{
public IUserInfoDal UserInfoDal { get ; set; } public void Show()
{
UserInfoDal.Show();
Console .WriteLine("it is service" );
}
}
<? xml version =" 1.0 " encoding =" utf-8 " ?>
< configuration>
< configSections>
< sectionGroup name = "spring " >
<!-- 这下面有context和object在下面都有对象的标签 -->
< section name = "context " type = "Spring.Context.Support.ContextHandler, Spring.Core " />
< section name = "objects " type = "Spring.Context.Support.DefaultSectionHandler, Spring.Core " />
</ sectionGroup >
</ configSections> < spring>
<!-- Spring.Net对象容器的配置 -->
< context >
<!-- 容器的所有的对象在哪里,这里用uri说明 -->
< resource uri = "config://spring/objects " />
<!-- 可以使用外部xml文件 --> </ context > <!-- objects:配置的容器里面的对象 -->
< objects xmlns = "http://www.springframework.net " >
< description >An example that demonstrates simple IoC features. </ description>
<!-- name最好用类名,type第一个是类的全称加上程序集,后面一个是项目名称 -->
< object name = "UserInfoDal " type = "SprintNetDemo.UserInfoDal, SprintNetDemo " >
<!-- 在这里是设置对象的属性,将Name的值设置成ctt -->
< property name = "Name " value = "ctt " />
</ object >
<!-- 在这里配置UserInfoService对象 -->
< object name = "UserInfoService " type = "SprintNetDemo.UserInfoService, SprintNetDemo " >
<!-- 在这里配置UserInfoService对象的UserInfoDal属性,执行上面产生的对象 -->
< property name = "UserInfoDal " ref = "UserInfoDal " />
</ object >
</ objects > </ spring> </ configuration>
static void Main( string[] args)
{
//IApplicationContext是Spring里面的一个超类,主要操作类
IApplicationContext ctx = ContextRegistry .GetContext();
//创建出对象,主要在配置文件中对UserInfoService的UserInfoDal属性进行复制
UserInfoService userInfoService = ctx.GetObject( "UserInfoService" ) as UserInfoService;
userInfoService.Show();
Console.ReadKey();
}
it is service
四 总结
Spring.Net 技术简介 IOC and DI的更多相关文章
- Spring入门一:IOC、DI、AOP基本思想
Spring框架是一个集众多涉及模式于一身的开源的.轻量级的项目管理框架,致力于javaee轻量级解决方案.相对于原来学过的框架而言,spring框架和之前学习的struts2.mybatis框架有了 ...
- Spring MVC -- Spring框架入门(IoC和DI)
Spring MVC是Spring框架中用于Web应用开发的一个模块.Spring MVC的MVC是Model-View-Controller的缩写.它是一个广泛应用于图像化用户交互开发中的设计模式, ...
- Spring框架中的IOC和DI的区别
上次面试被问到IOC和DI的区别时,没怎么在意,昨天又被问到,感觉有点可惜.今晚总算抽点时间,查看了spring官方文档.发现,IoC更像是一种思想,DI是一种行为.为了降低程序的耦合度,利用spri ...
- Spring系列三:IoC 与 DI
水晶帘动微风起,满架蔷薇一院香. 概述 在软件工程中,控制反转(IoC)是一种设计思想,对象之间耦合在一起,在运行时自动绑定,并且它们编译时对所需要引用的对象是不确定的.在这个spring教程中,通过 ...
- spring学习笔记之---IOC和DI
IOC和DI (一)IOC (1) 概念 IOC (Inverse of Control) 反转控制,就是将原本在程序中手动创建对象的控制权,交给spring框架管理.简单的说,就是创建对象控制权被反 ...
- Spring总结四:IOC和DI 注解方式
首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...
- Spring 注解方式 实现 IOC 和 DI
注:以下所有测试案例(最后一个除外)的测试代码都是同一个: package cn.tedu.test; import org.junit.Test; import org.springframewor ...
- 第四课:通过配置文件获取对象(Spring框架中的IOC和DI的底层就是基于这样的机制)
首先在D盘创建一个文件hero.txt,内容为:com.hero.Hero(此处必须是Hero的完整路径) 接下来是Hero类 package com.hero; public class Hero ...
- IOC and DI
Spring.Net 技术简介 IOC and DI 一 简单介绍 IOC 控制转移,就是将创建放到容器里,从而达到接耦合的目的,DI是 在容器创建对象的时候,DI读取配置文 ...
随机推荐
- JSP里比对单选框或复选框的数值而自动打勾
<table> <tr> <td class="tableleft">状态</td> <td><input typ ...
- Android 发送短信与接收短信
package com.example.testsms; import android.app.Activity; import android.app.PendingIntent; import a ...
- c 语言 结构体
一:结构体定义结构体类型变量 三种方式1st:先声明结构体类型,再定义该类型的变量struct student liming,zhangle;2nd:声明类型的同时定义变量struct student ...
- android- Auto Monitor Logcat
启动模拟器的时候弹出窗体: 它实在询问你是否显示logcat视图以便显示此工作空间中的程序信息. 因为如何程序错误,可以从logcat中看到错误的原因,建议选择yes. 单击确定,你会发现多了一个Lo ...
- SQL Server session故障排查
--根据作业名 查找session id select a.spid,a.blocked,b.name,substring(replace(a.PROGRAM_NAME,'SQLAgent - TSQ ...
- PHP自动生成后台导航网址的最佳方法
'http://www.jbxue.com'=> '脚本学堂首页', </script>
- SQL Server数据库的三种恢复模式:简单恢复模式、完整恢复模式和大容量日志恢复模式(转载)
SQL Server数据库有三种恢复模式:简单恢复模式.完整恢复模式和大容量日志恢复模式: 1.Simple 简单恢复模式, Simple模式的旧称叫”Checkpoint with truncate ...
- WebSocket简单介绍
Java后端WebSocket的Tomcat实现 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSoc ...
- crontab 日志备份定时任务
-l选项,查看当前用户的所有定时任务: [xiluhua@vm-xiluhua][/home]$ crontab -l * * * * * /home/xiluhua/shell_script/log ...
- require()与 require_once()、 include与include_once()