一,配置文件进行Spring初始化

1,配置文件编写

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
/************* 这里会报异常“Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常。把配置注释掉就行了
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
***************/
<configSections>
//初始化Spring
<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配置节点
<spring> <context>
<resource uri="config://spring/objects" />
</context> <objects xmlns="http://www.springframework.net">
<description>人类</description>
<object id="Model" type="Model.Person, Model" />
</objects> </spring> </configuration>

2,初始化代码

异常:No object named 'Person' is defined : Cannot find definition for object [Person]

 static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext(); IPerson dao = ctx.GetObject("Person") as IPerson; }

  

这里报异常是因为在配置文件中没有找到  <object id="Person" type="Model.Person, Model" />  的节点

需在项目中增加Spring.Core.dll的引用

二,使用Xml文件Spring初始化

(1)具体文件的初始化

编写Objects.xml

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd"> <object id="Person" type="Model.Person, Model"> </object> </objects>

后台初始化

        private static void ReadFromXml()
{
IResource input = new FileSystemResource("Objects.xml"); //路径----这里使用的是相对路径,也可以使用绝对路径,如果路径错了会报异常 IObjectFactory factory = new XmlObjectFactory(input); IPerson person = factory.GetObject("Person") as IPerson;        person.Speak("hello");
}

(2)程序集下寻找配置文件

  private static void ReadFromMuiltDoc()
{
//需满足URI语法。
//file://文件名
//assembly://程序集名/命名空名/文件名
string[] xmlFiles = new string[]
{
"file://Objects.xml"
}; IApplicationContext context = new XmlApplicationContext(xmlFiles); IObjectFactory factory = (IObjectFactory)context; IPerson person = factory.GetObject("Person") as IPerson; person.Speak("多文件初始化");
}

这种方式相对灵活

(3)在配置文件App.config或Web.config添加自定义配置节点

在配置文件中要引入<objects xmlns="http://www.springframework.net"/>命名空间,否则程序将会无法实例化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="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net"/> <!--必要-->
</spring>

这里<context>中的type是可选的

通过构造器创建对象

<object id="exampleObject"   type="Examples.ExampleObject, ExamplesLibrary"/>

type属性的格式:类型的全名,然后是一个逗号,最后是类型所在的程序集名称。

如果需要为嵌套类型创建对象,可以使用+号。例如,如果在类型Examples.ExampleObject嵌套定义了类型Person

<object id="exampleObject" type="Examples.ExampleObject+Person, ExamplesLibrary"/>

通过静态工厂方法创建对象

下面的对象就是通过静态工厂方法创建的。注意:对象定义中的type并非是要创建的对象的类型,而是包含了工厂方法的类型;同时,CreateInstance必须是静态方法。

<object id="exampleObject"
type="Examples.ExampleObjectFactory, ExamplesLibrary"
factory-method="CreateInstance"/>

通过实例工厂方法创建对象

如果要通过实例工厂方法创建对象,对象定义就不能包含type属性,而要用factory-object属性引用工厂方法所在的对象;注意,该属性值必须是包含工厂方法的对象的名称,且该对象必须定义在当前容器或父容器中。工厂方法的方法名则通过factory-method属性指定。

<!-- the factory object, which contains an instance method called 'CreateInstance' -->
<object id="exampleFactory" type="..."/>
<!-- the object that is to be created by the factory object -->
<object id="exampleObject"
factory-method="CreateInstance"
factory-object="exampleFactory"/>

Spring-----配置及对象初始化(1)的更多相关文章

  1. spring自动扫描、DispatcherServlet初始化流程、spring控制器Controller 过程剖析

    spring自动扫描1.自动扫描解析器ComponentScanBeanDefinitionParser,从doScan开始扫描解析指定包路径下的类注解信息并注册到工厂容器中. 2.进入后findCa ...

  2. Spring IoC容器的初始化过程

    Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...

  3. Spring配置汇总

    现在主流的JavaWeb应用几乎都会用到Spring,以下是Spring的配置,以及结合Web的SpringMVC配置的汇总. jar包的引入 与Web项目集成 Spring配置文件 SpringMV ...

  4. spring配置详解

    1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...

  5. 挖坟之Spring.NET IOC容器初始化

    因查找ht项目中一个久未解决spring内部异常,翻了一段时间源码.以此文总结springIOC,容器初始化过程. 语言背景是C#.网上有一些基于java的spring源码分析文档,大而乱,乱而不全, ...

  6. Spring 配置中的 default-lazy-init属性

    spring的容器是提供了lazy-load的,即默认的缺省设置是bean没有lazy-load,该属性处于false状态,这样导致spring在启动过程导致在启动时候,会默认加载整个对象实例图,从初 ...

  7. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  8. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  9. Spring之旅第三篇-Spring配置详解

    上一篇学习了IOC的概念并初步分析了实现原理,这篇主要学习Spring的配置,话不多说,让我们开始! 一.Bean元素配置 1.1 基本配置 看一个最基本的bean配置 <bean name=& ...

  10. Spring MVC之DispatcherServlet初始化详解

    Spring作为一个优秀的web框架,其运行是基于Tomcat的.在我们前面的讲解中,Spring的驱动都是使用的ClassPathXmlApplicationContext,并且都是直接在main方 ...

随机推荐

  1. 安卓程序代写 网上程序代写[原]Android项目中string.xml占位符

    开发中经常遇到这样的情况 , 在string.xml中用到以下占位符 <string name="delete_success">删除<xliff:g id=&q ...

  2. Android下ListView上下滑动背景变黑

    老问题,Google一下就能找到N多答案,为方便自己日后查阅,记录如下: 手指在ListView上下滚动时,ListViewItem背景变黑,因为在滚动的时候为了提升性能做了优化,为提高滚动的性能,A ...

  3. (笔记)AES加密在线计算工具

    AES加密在线计算工具: http://aes.online-domain-tools.com/

  4. 【吉比特】G-bits2018校园春季招聘技术类岗位笔试经验

    笔试公司:厦门吉比特网络技术股份有限公司 笔试岗位:游戏研发工程师 笔试时间:2018年3月30日19:00-20:30 笔试形式:牛客网在线做题 笔试回忆: 笔试总共时长1小时半,共52道题.其中选 ...

  5. Linux-HA实战(2)— TFS Nameserver HA之虚拟IP

    对TFS的Nameserver做机器级别的HA通过虚拟IP机制就可以了,只需要一个Heartbeat就可以搞定,下面简单说下步骤. 操作系统:CentOS 6.4 x86_64 Heartbeat: ...

  6. ASP.NET后台输出js脚本代码

    利用asp.net输出js我们大多数都会直接使用Respone.Write()然后根js格式的代码,再在页面调用时我们直接这样是完全可以实现的,下面我来给大家介绍另一种方法 我是我最初的想法以下是代码 ...

  7. C# Bitmap/png转成jpg格式,压缩图片

    public static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecIn ...

  8. spring cloud feign 上传文件报not a type supported by this encoder解决方案

    上传文件调用外部服务报错: not a type supported by this encoder 查看SpringFormEncoder类的源码: public class SpringFormE ...

  9. springboot+shiro+redis(集群redis版)整合教程

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...

  10. 【Web API系列教程】3.3 — 实战:处理数据(建立数据库)

    前言 在本部分中,你将在EF上使用Code First Migration来用測试数据建立数据库. 在Tools文件夹下选择Library Package Manager,然后选择Package Ma ...