Spring.NET主要包含Ioc、DI、Aop三个操作
IoC:Inversion of Control 即:控制反转
控制反转:把创建对象的权利有开发人员自己New,转到由容器来控制。IoC 不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合、更优良的程序
DI:Dependency Injection即:依赖注入
就是在通过容器创建对象的时候,在对象初始化的时候可以给一些属性、构造方法的参数等注入默认值,包括给复杂类型赋值。
解决的问题:解决了在项目开发过程中表现层(UI)与业务(BLL)耦合的问题。这只是一个测试Demo在复杂项目中 可用抽象工厂模式实现数据层与业务层解耦,而要实现业务层与表现层解耦则需要用到Spring框架的相关操作。
一、IoC实例:
1.构建一个winform项目(本例命名为Spring.NetDemo)
添加一个UserInfoService类和一个IUserInfoService接口(模拟实际项目中的业务层)
public interface IUserInfoService
{
string ShowMsg();
}
public class UserInfoService : IUserInfoService
{
public string ShowMsg()
{
return "Hello World";
}
}
2.在项目中添加引用
在项目文件夹同级目录下添加一个Lib文件夹
将 自己存放文件目录\Spring.Net\Spring.NET-2.0.0-M1\Spring.NET\bin\net\4.0\debug下的:
Spring.Aop.dll、Spring.Core.dll、Common.Logging.dll拷贝到Lib文件夹下,并在项目中添加引用
3.参考《Spring.NET框架参考文档》第二十五章. IoC快速入门 将app.config中配置添加和修改成如下内容
(附:《Spring.NET框架参考文档》链接:https://pan.baidu.com/s/12k_4tDVShmKNxxl1HaBBow)
<?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">
<!--在这里配置我们需要生成的实例对象 type构成为 第一个参数为命名空间+类名,第二个参数为命名空间名字-->
<object name="UserInfoService" type="Spring.NetDemo.UserInfoService, Spring.NetDemo"></object>
</objects>
</spring>
</configuration>
<!--这是参考文档中给出的Demo-->
<!--<?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">
<description>An example that demonstrates simple IoC features.</description>
</objects>
</spring>
</configuration>-->
4.在Buttorn中添加如下代码
private void button1_Click(object sender, EventArgs e)
{
//通过容器获得类的实例
IApplicationContext ctx = ContextRegistry.GetContext();//读取配置文件 拿到容器
IUserInfoService userInfoService = (IUserInfoService)ctx.GetObject("UserInfoService");//生成了UserInfoService的实例对象 UserInfoService继承于IUserInfoService
MessageBox.Show(userInfoService.ShowMsg());
}
5.测试本例结果
二、DI实例:
1.在项目UserInfoService类中添加一个UserName属性(用于实现给简单类型赋值)
2.在添加一个Persion类,包含一个Age属性(用于实现给复杂属性赋值)
3.在项目中添加一个service.xml的xml文档,添加一个单独的xml文档的原因是在具体项目中涉及到的业务类比较多,配置文件中<objects></objects>节的内容比较多,不利于书写和读取,因此将<objects></objects>节单独拿出放在一个xml文件中。
4.service.xml文件中写入如下配置(包括简单赋值和复杂赋值)
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<!--在这里配置我们需要生成的实例对象 type构成为 第一个参数为命名空间+类名,第二个参数为命名空间名字-->
<object name="UserInfoService" type="Spring.NetDemo.UserInfoService, Spring.NetDemo">
<property name="UserName" value="Lee"></property><!--此节点为简单赋初值 name可随意命名-->
<property name="Person" ref="MyPerson"></property><!--此节点为复杂类型的赋初值 name对象要关联要生成实例对象的名字 即此例子中下一个<object>节点 并在生成的实例对象中赋值 用ref关键字-->
</object>
<object name="MyPerson" type="Spring.NetDemo.Person, Spring.NetDemo">
<property name="Age" value="18"></property>
</object>
</objects>
5.在app.config文件中添加对service.xml的引用,主要要保留配置文件中的<objects>节点
<?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"/>
<!--添加对service.xml文件的引用-->
<resource uri="file://service.xml"/>
</context>
<objects xmlns="http://www.springframework.net">
</objects>
</spring>
</configuration>
6.修改service.xml文件属性为 如果较新则复制
7.测试赋值是否成功