- 首先创建一个控制台程序
要引入Spring.Core.dll 、Common.Logging.dll 和 antlr.runtime.dl 程序集
创建接口
public interface IUserInfoDal
{
void Show();
}
- 再然后创建两个类继承接口: AdoNetUserInfoDal、EFUserInfoDal
public class AdoNetUserInfoDal :Interface.IUserInfoDal
{
public void Show()
{
Console.WriteLine("我是 AdoNet Dal ");
}
}
public class EFUserInfoDal:Interface.IUserInfoDal
{
public void Show()
{
Console.WriteLine("我是EF Dal");
}
}
页面类图:
5、添加Spring.Net配置节点
<?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"></resource>
</context>
<objects xmlns="http://www.springframework.net">
<!--这里放容器里面的所有节点-->
<description>An example that demonstrates simple IoC features.</description>
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object name="UserInfoDal" type="ConsoleApplication1.Extend.EFUserInfoDal, ConsoleApplication1">
</object>
<object name="UserInfoDal2" type="ConsoleApplication1.Extend.AdoNetUserInfoDal, ConsoleApplication1">
</object>
<object name="UserInfo" type="ConsoleApplication1.Model.UserInfo,ConsoleApplication1">
</object>
</objects>
</spring>
</configuration>
控制台:
static void Main(string[] args)
{
//创建spring容器上下文
IApplicationContext ctx = ContextRegistry.GetContext();
//通过容器创建对象
IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
efDal.Show();
Console.ReadLine();
}
接下来能力提升, 要开始进行属性注入和构造器注入的功能。
IUserInfoDal 、 UserInfo 和AdoNetUserInfoDal 、EFUserInfoDal
public class UserInfo
{
public string Name { get; set; }
public string Age { get; set; }
}
public interface IUserInfoDal
{
UserInfo UserInfo { get; set; }
string Name { get; set; }
void Show();
}
public class AdoNetUserInfoDal :Interface.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);
}
}
public class EFUserInfoDal:Interface.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"></resource>
<!--xml文件方式,更改属性,复制到输出目录:始终复制-->
<!--<resource uri="file://objects.xml"/>-->
<!--嵌入程序集方式,assembly://程序集名/项目名/objects.xml,更改属性,始终复制,生成操作,嵌入的资源-->
<!--<resource uri="assembly://ConsoleApplication1/ConsoleApplication1/objects.xml"/>-->
</context>
<objects xmlns="http://www.springframework.net">
<!--这里放容器里面的所有节点-->
<description>An example that demonstrates simple IoC features.</description>
<!--name 必须要唯一的,type=类的全名称,所在的程序集-->
<object name="UserInfoDal" type="ConsoleApplication1.Extend.EFUserInfoDal, ConsoleApplication1">
<property name="Name" value="张三"></property>
<!--ref指向下面的属性注入-->
<property name="UserInfo" ref="UserInfo"></property>
</object>
<!--构造函数注入-->
<object name="UserInfoDal2" type="ConsoleApplication1.Extend.AdoNetUserInfoDal, ConsoleApplication1">
<constructor-arg index="0" value="张三"/>
<constructor-arg index="1" ref="UserInfo"/>
</object>
<!--属性注入-->
<object name="UserInfo" type="ConsoleApplication1.Model.UserInfo,ConsoleApplication1">
<property name="Name" value="李四"></property>
<property name="Age" value="16"></property>
</object>
</objects>
</spring>
</configuration>
控制台输入:
static void Main(string[] args)
{
//创建spring容器上下文
IApplicationContext ctx = ContextRegistry.GetContext();
//通过容器创建对象
IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
efDal.Show();
IUserInfoDal adoDal = ctx.GetObject("UserInfoDal2") as IUserInfoDal;
adoDal.Show();
Console.ReadLine();
}
所谓的张三和李四 这些名称, 是在app.config中填写到的。
如有不懂的问题欢迎咨询。
QQ:3260754258