Spring.Net学习笔记(3)-创建对象
一、开发环境
编译器:VS2013
.Net版本:.net framework4.5
二、涉及程序集
Spring.Core.dll:1.3
Common.Logging
三、开发过程
1.项目结构

2.添加Person.cs
namespace CreateObjects
{
public class Person
{
public override string ToString()
{
return "我是Person类";
} public class Heart
{
public override string ToString()
{
return "我是Heart类";
}
}
}
}
3.添加GenericClass.cs
namespace CreateObjects
{
public class GenericClass<T>
{
}
}
4.添加StaticObjectsFactory.cs
namespace CreateObjects
{
public static class StaticObjectsFactory
{
public static Person CreateInstance()
{
return new Person();
}
}
}
5.添加InstanceObjectsFactory.cs
namespace CreateObjects
{
public class InstanceObjectsFactory
{
public Person CreateInstance()
{
return new Person();
}
}
}
6.配置Spring.Net
<?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>
</context>
<objects>
<!--构造器-->
<object id="person" type="CreateObjects.Person,CreateObjects"></object>
<!--嵌套类型-->
<object id="heart" type="CreateObjects.Person+Heart,CreateObjects"></object> <!--泛型类型GenericClass<int>-->
<object id="genericClass" type="CreateObjects.GenericClass<int>,CreateObjects"></object> <!--静态工厂-->
<object id="staticObjectsFactory"
type="CreateObjects.StaticObjectsFactory,CreateObjects"
factory-method="CreateInstance" ></object> <!--实例工厂-->
<!--工厂-->
<object id="instanceObjectsFactory" type="CreateObjects.InstanceObjectsFactory,CreateObjects"></object>
<!--创建的对象-->
<object id="instancePerson" factory-method="CreateInstance" factory-object="instanceObjectsFactory"></object> </objects>
</spring>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
7.控制台程序如下
namespace CreateObjects
{
class Program
{
static void Main(string[] args)
{
IApplicationContext context = ContextRegistry.GetContext();
Person person = context.GetObject("person") as Person;
Console.WriteLine(person); Person.Heart heart = context.GetObject("heart") as Person.Heart;
Console.WriteLine(heart); GenericClass<int> genericClass = context.GetObject("genericClass") as GenericClass<int>;
Console.WriteLine(genericClass); Person personStaticFactory = context.GetObject("staticObjectsFactory") as Person;
Console.WriteLine(personStaticFactory); Person personInstanceFactory = context.GetObject("instancePerson") as Person;
Console.WriteLine(personInstanceFactory);
Console.ReadKey();
}
}
}
Spring.Net学习笔记(3)-创建对象的更多相关文章
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
- spring揭密学习笔记
spring揭密学习笔记 spring揭密学习笔记(1) --spring的由来 spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息
</pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...
随机推荐
- Delphi XE4 inline 的用法
为了提高应用程序的性能,可以用inline声明一个函数过过程,对于声明为inline的过程,编译器在编译时,会取代正常的过程调用方式,取而代之是直接把这个函数的编译代码拿过来生成到调用该过程的地方,有 ...
- POJ1068 Parencodings 解题报告
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- POJ——T 2976 Dropping tests
http://poj.org/problem?id=2976 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13861 ...
- MongoDB小结08 - update【$pull】
它可以删除所匹配的值,如果[1,1,2,1] 执行pull 1 后,只剩下[2]
- c++11中的线程、锁和条件变量
void func(int i, double d, const string& s) { cout << i << ", " << d ...
- 【转】golang中的并行与并发
原文:http://blog.csdn.net/taohaoge/article/details/27970421 ------------------------------------------ ...
- Spring Boot中微信全局token的缓存实现
为什么要缓存token? 这里的token指的是微信JSAPI中基础支持的ACCESS_TOKEN,并非网页授权ACCESS_TOKEN.网页授权Token每天的调用次数没有限制,不需要缓存. 接口 ...
- 通过android XML 创建图形,降低对美工的依赖
在开发中总会须要自己定义各种View的样式,假设总是依赖美工作图弄出须要的UI样式图片(比方:一个button要选中和默认两张图片),不但时间上会浪费.往往也会有适配问题. 尽管能够通过.9图来解决一 ...
- 基于OAS设计可扩展OpenAPI
前言 随着互联网行业的兴起,开发模式已逐步转换为微服务自治:小团队开发微服务,然后通过Restful接口相互调用.开发者们越来越渴望能够使用一种“官话”进行流畅的沟通,甚至实现多种编程语言系统的自动化 ...
- Ubuntu虚拟机安装遇到的各种坑
配置 13年Macbook Pro 虚拟机环境 Parallels Desktop Linux 版本 Ubuntu 16.04 1.分辨率问题 进入只有一种分辨率 终端输入 sudo xdiagnos ...