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>& ...
随机推荐
- Ajax核心知识(1)
XMLHttpRequest对象创建 所有现代浏览器均支持XMLHttpRequest对象( IE5 和 IE6 使用 ActiveXObject). XMLHttpRequest用于在后台与服务器交 ...
- operamasks—omBorderLayout布局
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 保持WCF服务端与客户端的长连接
背景 客户端与服务端使用WCF建立连接后:1.可能长时间不对话(调用服务操作):2.客户端的网络不稳定. 为服务端与客户端两边都写“心跳检测”代码?不愿意. 解决 设置inactivityTimeou ...
- golang convert integer to float number
There is no float type. Looks like you want float64. You could also use float32 if you only need a s ...
- [Web Analytics] Into to Web Analytics
Just to get started for myself. Any developer who doesn't care about the business is not a good soft ...
- [React] Preview and edit a component live with React Live
In this lesson we'll use React Live to preview and edit a component directly in the browser. React L ...
- [Java Sprint] Spring XML Configuration : Setter Injection Demo
In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...
- android:“新版飞机大战”源码开源啦!
今天10.24,为了纪念程序猿的节日,把之前写过的一个"飞机大战"的一个源码开源了. 源码地址:https://github.com/nuptboyzhb/newplanegame ...
- LintCode 近期公共祖先
中等 近期公共祖先 查看执行结果 34% 通过 给定一棵二叉树,找到两个节点的近期公共父节点(LCA). 近期公共祖先是两个节点的公共的祖先节点且具有最大深度. 您在真实的面试中是否遇到过这个题? Y ...
- Eclipse或SVN—怎样在Eclipse中安装SVNclient插件
1.下载SVN插件,下载地址:http://download.csdn.net/download/wangshuxuncom/7638615 2.解压下载下来的压缩文件然后依据里面名为"使用 ...