初入 Spring.net

IOC:Inversion Of Control

控制翻转:就是创建对象的权利由开发人员自己控制,转换到了由容器来控制

我用的是winform里的一个按键来触发的

首先要引入两个Spring.net的dll文件Common.Logging.dll 和Spring.Core.dll

初入 Spring.net
    private void button1_Click(object sender, EventArgs e)
        {
            
            IApplicationContext ctx = ContextRegistry.GetContext();
            UserInfoService userInfo = (UserInfoService)ctx.GetObject("UserInfoService");
            userInfo.Show();
        }
初入 Spring.net
初入 Spring.net
    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); } } }

初入 Spring.net
初入 Spring.net
//user类型
namespace
SpringDemo { public class UserInfo { public string uName { get; set; } } }
初入 Spring.net

之后就直接在app.config里注入代码就可以了

初入 Spring.net
<?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>
初入 Spring.net

然后调试就完美输出了

OK2---------yang

Age=18

User=yang2

 

从现在开始一点点进步

初入 Spring.net,布布扣,bubuko.com

初入 Spring.net

上一篇:把一个数最右边那一位1弄出来


下一篇:Pane窗格,Panel面板,getContentPane()