Spring.Net学习笔记(4)-属性及构造器注入
一、开发环境
操作系统: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)-属性及构造器注入的更多相关文章
- Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200
1.person类 public class Person { public string Name { get; set; } public int Age { g ...
- Spring.NET学习笔记8——集合类型的注入(基础篇)
1.基础类 public class Happy { public override string ToString() { return &q ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录
Spring MVC 学习笔记8 -- 实现简单的用户管理(4)用户登录 增删改查,login 1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; &g ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
随机推荐
- Delphi ADO的Lookup类型字段的问题
关于ADO数据集控件中的Lookup类型字段,在其Lookupkeyfields属性指向的字段中存在NULL值的,就会出现'EOleException with message '发生未知错误',这个 ...
- activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自己定义流程之Spr ...
- Zabbix ---proxy 代理
Zabbix zabbix 官网 : https://www.zabbix.com/ 环境准备: 三台服务器: server 端: 192.168.206.6 proxy 端 : 192.168.2 ...
- Matplotlib作图基础
折线图 import matplotlib.pylab as pylab import numpy as npy x=[1,2,3,4,8] y=[5,7,2,1,5] #折线图 pylab.plot ...
- Android处理日期
近期做一个项目,后台返回的日期是RFC3339格式的.之前没有看到过,当中遇到了几个问题以及解决 1.2015-11-18T14:49:55Z转换 在SimpleDateFormat中给出了几种格式 ...
- Android WebView开发常见问题
1.加入权限:AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web page not available错 ...
- java图片处理工具之-ImageMagick+jmagick(二)
简单的图片处理測试类: public class ImageUtil { static{ System.setProperty("jmagick.systemclassl ...
- python 矩阵
python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包. 1.numpy的导入和使用 from numpy import *;#导入numpy的库函数 im ...
- ZOJ3962 2017 E.Seven Segment Display
数码管从某个状态顺序转移N个状态 计算总共有多少个数码管被点亮 N<=10^9 观察数码管的变化规律,有明显的周期和重复,利用这个性质,计算相对于初始状态,某一位上的某个状态重复了多少次,就可以 ...
- 洛谷P2680 运输计划——树上差分
题目:https://www.luogu.org/problemnew/show/P2680 久违地1A了好高兴啊! 首先,要最大值最小,很容易想到二分: 判断当前的 mid 是否可行,需要看看有没有 ...