一、开发环境

操作系统: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");
}
}
}

Spring.Net学习笔记(4)-属性及构造器注入的更多相关文章

  1. Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200

    1.person类 public class Person    {        public string Name { get; set; }        public int Age { g ...

  2. Spring.NET学习笔记8——集合类型的注入(基础篇)

    1.基础类 public class Happy    {        public override string ToString()        {            return &q ...

  3. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  4. Spring.NET学习笔记——目录(原)

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  5. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  6. Spring MVC 学习笔记11 —— 后端返回json格式数据

    Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...

  7. Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录

    Spring MVC 学习笔记8 -- 实现简单的用户管理(4)用户登录 增删改查,login 1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; &g ...

  8. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  9. Spring框架学习笔记(1)

    Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...

随机推荐

  1. Model、ModelMap、ModelAndView的使用和区别

    1.Model的使用 数据传递:Model是通过addAttribute方法向页面传递数据的: 数据获取:JSP页面可以通过el表达式或C标签库的方法获取数据: return:return返回的是指定 ...

  2. 在Windows上安装Nexus

    在Windows上安装Nexus 学习了:https://www.cnblogs.com/yucongblog/p/6696736.html 下载地址:https://sonatype-downloa ...

  3. CentOS 5.11开启VNC Service

    1.     #yum install vncserver 2.     #vncpasswd       此密码将成为vnc的login password          password:    ...

  4. CNN卷积神经网络的改进(15年最新paper)

    回归正题,今天要跟大家分享的是一些 Convolutional Neural Networks(CNN)的工作. 大家都知道,CNN 最早提出时,是以一定的人眼生理结构为基础,然后逐渐定下来了一些经典 ...

  5. TMS320F28335项目开发记录6_28335之cmd文件具体解释

    1.CMD文件的作用 CMD文件的作用就像仓库的货物摆放记录一样,为程序代码和数据分配指定的空间. 2.C语言生成的段 C语言生成的段大致分为两大类:初始化和未初始化,已初始化的段含有真正的指令和数据 ...

  6. Android仿微信朋友圈图片查看器

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,细致观察是不是发 ...

  7. Objective-C 内存管理retain和release

    OC使用引用计数来管理内存,每个继承NSObject的对象,内部都维护了一个引用计数器retainCount.当对象创建时(调用alloc或者new)引用计数器会+1, 手动调用retain()方法能 ...

  8. iOS开发——优化篇—— 25个性能优化/内存优化常用方法

    1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为你 ...

  9. sql compare options

    sql compare project's options Add object existence checks Use DROP and CREATE instead of ALTER Ignor ...

  10. BZOJ_2160_拉拉队排练_manacher

    BZOJ_2160_拉拉队排练_manacher Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛 ...