初入 Spring.net
2024-09-27 19:19:51
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=""/>//--------构造函数注入
<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
从现在开始一点点进步
最新文章
- php-7.1.0 rpm包制作
- js字符串格式化扩展方法
- linux php 安装 memcache 扩展
- While循环
- Windows Azure 虚拟网络配置(Point to Site)
- Sublime Text 前端开发常用扩展插件推荐
- java多线程总结二:后台线程(守护线程)
- pfsense 2.2RC下的L2TP配置
- 从Qt4到Qt5的,主要的进化有三(对于QtWidget的精简和优化会很有限)
- java中多态的使用
- 使用动态内置的JSON 数据源
- python flask(多对多表查询)
- Python连接MySQL数据库中各种坑
- 腾讯windows系统服务器
- 解决SoapFault (looks like we got no XML document)问题
- [Python监控]psutil模块简单使用
- UITableView的分割线长短的控制
- 关于mybatis缓存配置讲解
- Fibonacci (hdu1568)数学公式
- c# 实现获取汉字十六进制Unicode编码字符串
热门文章
- Codeforces 474C Captain Marmot 给定4个点和各自旋转中心 问旋转成正方形的次数
- 【RequireJS--API学习笔记】
- AngularJS Directive - 开场小介绍(转)
- php 二维码生成类
- Android开发记录(转)
- asp.net 页面上传文件控件后台代码Request.Files获取不到
- hitTest:withEvent:方法(此方法可实现点击穿透、点击下层视图功能)
- I - u Calculate e
- NYOJ 45 棋盘覆盖
- Js之Location对象