Spring.Net---1、IOC第一个实例
Spring.NET IoC容器的用法。
通过简单的例子学习Spring.Net
1、先创建一个控制台程序项目。
2、添加IUserInfoDal 接口。

namespace Spring.Net
{
public interface IUserInfoDal
{
void Show();
}
}

3、添加AdoNetUserInfoDal类和EFUserInfoDal类,继承IUserInfoDal接口。
AdoNetUserInfoDal.cs

public class AdoNetUserInfoDal:IUserInfoDal
{
public void Show()
{
Console.WriteLine("我是 AdoNet Dal );
}
}

EFUserInfoDal.cs

public class EFUserInfoDal: IUserInfoDal
{
public void Show()
{
Console.WriteLine("我是EF Dal);
}
}

4、引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll
5、添加Spring.Net配置节点,配置object节点

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--一定要在紧跟着configuration下面添加-->
<configSections>
<!--跟下面Spring.Net节点配置是一一对应关系-->
<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> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <!--Spring.Net节点配置-->
<spring>
<context>
<!--容器配置-->
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<!--这里放容器里面的所有节点-->
<description>An example that demonstrates simple IoC features.</description>
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net">
</object>
</spring>
</configuration>

6、开始写主函数,创建spring容器上下文

namespace Spring.Net
{
class Program
{
static void Main(string[] args)
{
//控制权没有反转
//IUserInfoDal infoDal = new EFUserInfoDal(); //Spring.Net 创建实例的方式转为容器帮我们创建
//创建spring容器上下文
IApplicationContext ctx = ContextRegistry.GetContext();
//通过容器创建对象
IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
efDal.Show();
Console.ReadKey();
}
}
}

7、属性注入

<!--Spring.Net节点配置-->
<spring>
<context>
<!--容器配置-->
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<!--这里放容器里面的所有节点-->
<description>An example that demonstrates simple IoC features.</description>
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net">
<property name="Name" value="张三"/>
<!--ref指向下面的属性注入-->
<property name="UserInfo" ref="UserInfo"/>
</object> <!--属性注入-->
<object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net">
<property name="Name" value="李四"/>
<property name="Age" value="15"/>
</object>
</objects>
</spring>

8、构造函数注入
<!--构造函数注入-->
<object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net">
<constructor-arg index="0" value="张三"/>
<constructor-arg index="1" ref="UserInfo"/>
</object>
9、容器配置

<context>
<!--容器配置-->
<resource uri="config://spring/objects"/>
<!--xml文件方式,更改属性,复制到输出目录:始终复制-->
<!--<resource uri="file://objects.xml"/>-->
<!--嵌入程序集方式,assembly://程序集名/项目名/objects.xml,更改属性,始终复制,生成操作,嵌入的资源-->
<!--<resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>-->
</context>

10、完整例子
IUserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Spring.Net
{
public interface IUserInfoDal
{
UserInfo UserInfo { get; set; }
string Name { get; set; } void Show();
}
}

AdoNetUserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Spring.Net
{
public class AdoNetUserInfoDal:IUserInfoDal
{
public AdoNetUserInfoDal(string name, UserInfo userInfo)
{
Name = name;
UserInfo = userInfo;
}
public UserInfo UserInfo { get; set; }
public string Name { get; set; } public void Show()
{
Console.WriteLine("我是 AdoNet Dal ,属性注入:Name=" + Name);
Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age);
}
}
}

EFUserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Spring.Net
{
public class EFUserInfoDal: IUserInfoDal
{
public EFUserInfoDal()
{ }
public UserInfo UserInfo { get; set; }
public string Name { get; set; } public void Show()
{
Console.WriteLine("我是EF Dal,属性注入:Name=" + Name);
Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age);
}
}
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--一定要在紧跟着configuration下面添加-->
<configSections>
<!--跟下面Spring.Net节点配置是一一对应关系-->
<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> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <!--Spring.Net节点配置-->
<spring>
<context>
<!--容器配置-->
<resource uri="config://spring/objects"/>
<!--xml文件方式,更改属性,复制到输出目录:始终复制-->
<!--<resource uri="file://objects.xml"/>-->
<!--嵌入程序集方式,assembly://程序集名/项目名/objects.xml,更改属性,始终复制,生成操作,嵌入的资源-->
<!--<resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>-->
</context>
<objects xmlns="http://www.springframework.net">
<!--这里放容器里面的所有节点-->
<description>An example that demonstrates simple IoC features.</description>
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net">
<property name="Name" value="张三"/>
<!--ref指向下面的属性注入-->
<property name="UserInfo" ref="UserInfo"/>
</object> <!--构造函数注入-->
<object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net">
<constructor-arg index="0" value="张三"/>
<constructor-arg index="1" ref="UserInfo"/>
</object> <!--属性注入-->
<object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net">
<property name="Name" value="李四"/>
<property name="Age" value="15"/>
</object>
</objects>
</spring>
</configuration>

Program.cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; //Spring.Aop.dll 面向切面编程
//Spring.Core.dll spring框架基础
//Common.Logging.dll 这个必须也要引用 namespace Spring.Net
{
class Program
{
static void Main(string[] args)
{
//控制权没有反转
//IUserInfoDal infoDal = new EFUserInfoDal(); //Spring.Net 创建实例的方式转为容器帮我们创建
//第一步,引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll
//第二步,添加Spring.Net配置节点
//第三步,配置object节点
//第四步,创建spring容器上下文
IApplicationContext ctx = ContextRegistry.GetContext();
//第五步,通过容器创建对象
IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
efDal.Show(); IUserInfoDal adoDal = ctx.GetObject("UserInfoDal2") as IUserInfoDal;
adoDal.Show(); Console.ReadKey();
}
}
}

下面是MVC项目中使用Spring.Net融合的例子
1、将Spring.net程序集复制到项目下的packages文件夹中。
2、项目中添加引用Spring.Core.dll、Common.Logging.dll、Spring.Web.Mvc4.dll、Spring.Web.dll
3、修改Web.config,配置节点

<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- Spring.Net配置节点 -->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
</sectionGroup>
</configSections>
<!-- Spring.Net节点详细配置 -->
<spring>
<context>
<resource uri="file://~/Config/controllers.xml"/>
</context>
</spring> </configuration>

4、项目中创建Config文件夹,controllers.xml文件。

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"> <object type="HX.Shop.UI.Main.Controllers.AdminController, HX.Shop.UI.Main" singleton="false" >
<property name="AdminService" ref="AdminService" />
</object>
<object name="AdminService" type="HX.Shop.BLL.AdminService, HX.Shop.BLL" singleton="false" > </object>
</objects>

5、Global.asax 中修改 SpringMvcApplication
public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication //System.Web.HttpApplication
Spring.Net---1、IOC第一个实例的更多相关文章
- Spring学习笔记IOC与AOP实例
Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...
- Spring FrameWork4(MVC + IOC)高速入门实例
使用Maven创建project并配置依赖项 首先创建一个Maven Project: 然后选择创建Maven 的webapp实例,当然也能够通过命令行方式创建Maven webapp的项目再转化并导 ...
- Spring中IoC的入门实例
Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...
- Spring入门1. IoC入门实例
Spring入门1. IoC入门实例 Reference:Java EE轻量级解决方案——S2SH 前言: 之前学习过关于Spring的一点知识,曾经因为配置出现问题,而总是被迫放弃学习这些框架技术, ...
- Spring源码-IOC部分-循环依赖-用实例证明去掉二级缓存会出现什么问题【7】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- Spring 简单使用IoC与DI——XML配置
目录 Spring简介 导入jar包 Spring配置文件 Spring的IoC IoC简介 快速使用IoC Spring创建对象的三种方式 使用构造方法 使用实例工厂 使用静态静态工厂 Spring ...
- spring学习(一) ———— IOC讲解
spring基本就两个核心内容,IOC和AOP.把这两个学会了基本上就会用了. --WH 一.什么是IOC? IOC:控制反转,通俗点讲,将对象的创建权交给spring,我们需要new对象,则由spr ...
- Spring的AOP配置文件和注解实例解析
1.1 Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...
- Spring中的IOC
在学习spring的时候,最常听到的词应该就是IOC和AOP了,以下,我从我的角度再次理解一下Spring里的IOC和AOP. IOC简单介绍 IoC(InversionofControl):IoC就 ...
随机推荐
- 【OCP-12c】CUUG 071题库考试原题及答案解析(13)
13.(6-7) choose twoWhich two statements are true regarding operators used with subqueries? (Choose t ...
- iOS仿UC浏览器顶部频道滚动效果
很喜欢用UC浏览器上网,当然不是给UC打广告,里面有很多酷炫的效果,值的学习,这次分享的是频道滚动的效果.动画效果如下: 实现的这个效果的关键是绘制,重写顶部Label的drawRect方法 gith ...
- 【BZOJ2084】【洛谷P3501】[POI2010]ANT-Antisymmetry(Manache算法)
题意描述 原题: 一句话描述:对于一个0/1序列,求出其中异或意义下回文的子串数量. 题解 我们可以看出,这个其实是一个对于异或意义下的回文子串数量的统计,什么是异或意义下呢?平常,我们对回文的定义是 ...
- 【bash】今天你坑队友了吗
需求: 压缩日志并删除压缩过的文件 很日常的运维需求!!! 好,来看代码 echo 'start' quke.log rm -f quke.log echo 'delete' 不管是初级运维还是高级运 ...
- javascript解决getElementById()的bug以及getElementsByClassName的兼容性写法
<a name="target" href="#">链接</a> <p id="target">文字说明 ...
- jmeter结果分析(图形报表和聚合报告)
采用Jmeter测试工具对web系统作的负载测试,得出的响应报表,数据比较难懂,现作一具体说明.以下是在一次具体负载测试中得出的具体数值,测试线程设置情况为:线程数:200,等待时间(ramp-up) ...
- 个人KPI制定
1.工作量 1.1 能独立完成工作优先级 1.2 能独立预估工作时间 2.工作质量 2.1 项目按时完成没有延期 2.2 交付件质量 2.2.1 测试用例设计没有明显遗漏 2.2.2 测试bug符合规 ...
- centos启动错误:Inodes that were part of a corrupted orphan linked list found.
centos启动时,提示错误: /dev/mapper/VolGroup-lv_root contains a file system with errors,check forced. /dev/m ...
- VUE 项目dependency was not found: * !!vue-style-loader!css-loader? 解决方案
用npm run dev 运行vue项目时,出现以下错误: ERROR Failed to compile with errors :: This dependency was not found: ...
- jmeter之beanshell断言实例
.首先储存一个接口的响应结果,比如在http请求的后面添加beanshell后置处理器(BeanShell PostProcessor)来储存http请求的响应结果: import org.json. ...