
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 初入Spring.net
IOC:Inversion Of Control
控制翻转:就是创建对象的权利由开发人员自己控制,转换到了由容器来控制
我用的是winform里的一个按键来触发的
首先要引入两个SPRing.net的dll文件Common.Logging.dll 和Spring.Core.dll
private void button1_Click(object sender, EventArgs e)
{
IapplicationContext ctx = ContextRegistry.GetContext();
UserInfoService userInfo = (UserInfoService)ctx.GetObject("UserInfoService");
userInfo.Show();
}
public class UserInfoService
{
public UserInfo user { get; set; }
//构造函数
public UserInfoService(int age)
{
Age = age;
}
public int Age { get; set; }
public string serverName { get; set; }
public void Show()
{
Console.WriteLine("OK2------"+serverName);
Console.WriteLine("Age=" + Age);
Console.WriteLine("User=" + user.uName);
}
}
}
//user类型
namespace SpringDemo { public class UserInfo { public string uName { get; set; } } }
之后就直接在app.config里注入代码就可以了
<?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"/>
<resource uri="file://Servicess.xml"/>
</context>
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description>
<object name="UserInfoService" type="SpringDemo.UserInfoService, SpringDemo ">
<constructor-arg name="age" value="18"/>//--------构造函数注入
<property name="serverName" value="yang"/>//------------属性注入
<property name="user" ref="UserInfo"/>
</object>
<object name="UserInfo" type="SpringDemo.UserInfo, SpringDemo ">//-----user类型的注入
<property name="uName" value="yang2"/>
</object>
</objects>
</spring>
</configuration>
然后调试就完美输出了
OK2---------yang
Age=18
User=yang2
从现在开始一点点进步