一、开发环境

操作系统:Win10

编译器:VS2013

.Net版本:.net framework4.5

二、涉及程序集

Spring.Core.dll:1.3.1

Common.Logging.dll

三、开发过程

1.项目结构

2.编写Product.cs

namespace SpringNetDi
{
public class Product
{
public string Name { get; set; } public decimal UnitPrice { get; set; }
}
}

3.编写ProductFactory.cs

namespace SpringNetDi
{
public class ProductFactory
{
public Product FactoryProduct { get; set; }
}
}

4.编写Article.cs

namespace SpringNetDi
{
public class Article
{
private string name;
private string writer; public Article(string name, string writer)
{
this.name = name;
this.writer = writer;
} public void GetMsg()
{
Console.WriteLine("你好,我是" + writer);
}
}
}

5.配置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>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
</sectionGroup>
</configSections> <spring>
<context>
<resource uri="config://spring/objects"></resource>
</context>
<objects>
<!--01属性注入-值类型-->
<object name =" product" type="SpringNetDi.Product,SpringNetDi">
<property name="Name" value="记号笔"></property>
<property name="UnitPrice" value="5"></property>
</object>
<!--02属性注入-引用类型-->
<object name="factory" type="SpringNetDi.ProductFactory,SpringNetDi">
<property name="FactoryProduct" ref="product"></property>
</object>
<!--03构造函数注入-->
<object name="article" type="SpringNetDi.Article,SpringNetDi">
<constructor-arg name="name" value="依赖注入"></constructor-arg>
<constructor-arg name="writer" value="Kimisme"></constructor-arg>
</object>
</objects>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

6.控制台代码

namespace SpringNetDi
{
class Program
{
static void Main(string[] args)
{
IApplicationContext context = ContextRegistry.GetContext();
Product product = context.GetObject("product") as Product;
Console.WriteLine(product.Name); ProductFactory factory = context.GetObject("factory") as ProductFactory;
Console.WriteLine(factory.FactoryProduct.Name); Article article = context.GetObject("article") as Article;
article.GetMsg();
Console.WriteLine("ok");
}
}
}

最新文章

  1. jQuery 事件方法
  2. Window远程连接Linux系统(CentOS7)
  3. SQL Server 2008 定时作业的制定(SQL2005参考此方法) 转
  4. lightoj1038
  5. python内存管理
  6. [LeetCode]题解(python):124-Binary Tree Maximum Path Sum
  7. OGR – Merging Multiple SHP files
  8. elsticsearch在kibanna中的操作
  9. 什么?你竟然还没有用这几个chrome插件?
  10. VMware与CentOS的安装与Linux简单指令
  11. 深度学习模型融合stacking
  12. Go语言学习之5 进阶-排序、链表、二叉树、接口
  13. Confluence 6 删除和归档空间
  14. MySQL Keynote
  15. java 装饰者模式
  16. Dell笔记本Ubuntu无线网卡驱动安装
  17. sqlachemy中批量删除的问题
  18. DataTable的AcceptChanges()和RejectChanges()方法
  19. 原生js写的一个简单slider
  20. dubbo服务治理中间件,zookeeper注册中心 安装配置

热门文章

  1. Ubuntu清理内存命令(效果不明显)
  2. HDU 1030 数学题
  3. Office Excel找不到PERSONAL.XLS怎么办
  4. vux 全局使用 loading / toast / alert
  5. 【hdu3544】 Alice&#39;s Game
  6. 一起talk C栗子吧(第九十回:C语言实例--使用管道进行进程间通信三)
  7. Linux 系统内核空间与用户空间通信的实现与分析
  8. bzoj3550: [ONTAK2010]Vacation&amp;&amp;bzoj3112: [Zjoi2013]防守战线
  9. oracle数据库的导入 导出实例
  10. ZOJ 3955 Saddle Point 校赛 一道计数题