SPRING.NET 配置对象属性注入的例子
来源:互联网 发布:华大基因ipo数据撒谎 编辑:程序博客网 时间:2024/05/30 13:42
有几年没用spring.net了,下星期打算在项目里面用一下,毕竟注入能力太强大了。
首先还是用NuGet引用一下 Install-Package Spring.Core
下面创建三个类,其中一个类包含另外两个类作为属性
namespace SpringNetPropertyInject{ public class GameChannel { public string ChannelName { set; get; } public string ChannelId { set; get; } }}namespace SpringNetPropertyInject{ public class ChannelAccount { public string AccountName { set; get; } public string AccountId { set; get; } }}namespace SpringNetPropertyInject{ public class Test { public GameChannel Channel { set; get; } public ChannelAccount Account { set; get; } public void ShowName() { MessageBox.Show(Channel.ChannelName + Account.AccountName); } }}
我不太喜欢在app.config文件里面配置东西,因为会把那个文件搞得很臃肿,下面用object.xml文件进行配置
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net"> <object id="GameChannel" type="SpringNetPropertyInject.GameChannel"> <property name="ChannelName" value="某某渠道"/> </object> <object id="ChannelAccount" type="SpringNetPropertyInject.ChannelAccount"> <property name="AccountName" value="某某账户"/> </object> <object id="Test" type="SpringNetPropertyInject.Test"> <property name="Channel" ref="GameChannel"/> <property name="Account" ref="ChannelAccount"/> </object></objects>
值得注意的是这两句
<property name="Channel" ref="GameChannel"/> <property name="Account" ref="ChannelAccount"/>
Channel 为Test类里面的属性名称,它的注入配置节叫GameChannel,也就是下面这个
<object id="GameChannel" type="SpringNetPropertyInject.GameChannel"> <property name="ChannelName" value="某某渠道"/> </object>
这样把对象属性的属性也注入了,接着在程序中调用
using System;using System.Windows.Forms;using Spring.Context.Support;namespace SpringNetPropertyInject{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { var context = new XmlApplicationContext(Application.StartupPath + @"\object.xml"); var test = context.GetObject("Test") as Test; if (test != null) test.ShowName(); } }}
可以看到,对象已被成功注入了
0 0
- SPRING.NET 配置对象属性注入的例子
- Spring对象属性注入
- Spring 对象属性注入
- spring对象类型属性的注入
- Spring注入对象类型的属性
- Spring各种属性的注入配置
- SPRING.NET 1.3.2 学习21--使用对象或类的属性值进行注入
- Spring容器配置注入List集合对象属性
- (二)spring的注入---配置构造器属性注入
- spring的注入参数(bean对象中又有bean属性),beans.xml配置
- Spring自定义对象的注入配置
- spring注入对象类型属性
- spring注入properties属性配置
- Spring 相关配置&属性注入
- Spring---->属性的注入
- NET MVC+Spring.NET:二、注入自定义的对象
- 八.Spring的属性注入-注入对象,复杂的集合类型属性
- Spring中的DI(依赖注入)--给对象的属性赋值
- JPEG文件编/解码详解
- IE6、 IE7、IE8、IE9、Firefox兼容性问题
- 好东西得收藏
- php将表单信息传入数据库
- 老伴说我每个标点符号后都打一个空格, 我才意识到真有这个问题,其实就是个职业病吧
- SPRING.NET 配置对象属性注入的例子
- POJ 2965 The Pilots Brothers' refrigerator(反转)
- 关于脚本
- 括号匹配问题
- [LeetCode]Letter Combinations of a Phone Number
- 黑马程序员 -----------网络编程
- pycharm
- NYOJ 217 a letter and a number
- 神经网络的基础学习