Spring-----配置及对象初始化(1)
一,配置文件进行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)的更多相关文章
- spring自动扫描、DispatcherServlet初始化流程、spring控制器Controller 过程剖析
spring自动扫描1.自动扫描解析器ComponentScanBeanDefinitionParser,从doScan开始扫描解析指定包路径下的类注解信息并注册到工厂容器中. 2.进入后findCa ...
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- Spring配置汇总
现在主流的JavaWeb应用几乎都会用到Spring,以下是Spring的配置,以及结合Web的SpringMVC配置的汇总. jar包的引入 与Web项目集成 Spring配置文件 SpringMV ...
- spring配置详解
1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...
- 挖坟之Spring.NET IOC容器初始化
因查找ht项目中一个久未解决spring内部异常,翻了一段时间源码.以此文总结springIOC,容器初始化过程. 语言背景是C#.网上有一些基于java的spring源码分析文档,大而乱,乱而不全, ...
- Spring 配置中的 default-lazy-init属性
spring的容器是提供了lazy-load的,即默认的缺省设置是bean没有lazy-load,该属性处于false状态,这样导致spring在启动过程导致在启动时候,会默认加载整个对象实例图,从初 ...
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
- Spring -- 配置bean的三种方法
配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...
- Spring之旅第三篇-Spring配置详解
上一篇学习了IOC的概念并初步分析了实现原理,这篇主要学习Spring的配置,话不多说,让我们开始! 一.Bean元素配置 1.1 基本配置 看一个最基本的bean配置 <bean name=& ...
- Spring MVC之DispatcherServlet初始化详解
Spring作为一个优秀的web框架,其运行是基于Tomcat的.在我们前面的讲解中,Spring的驱动都是使用的ClassPathXmlApplicationContext,并且都是直接在main方 ...
随机推荐
- android开发(36) Android WebView背景设置为透明
xml布局 <WebView android:id="@+id/wv_content" android:layout_width="match_parent&quo ...
- 【Ansible】 各种模块
[Ansible 模块] 就如python库一样,ansible的模块也分成了基本模块和第三方拓展模块(自定义的模块).这些模块其实才是作为真实的逻辑载体,在帮助ansible进行作业. ansibl ...
- sparkr脚本
test <- function(){ print(1) } test() 脚本内为单独函数时 调用脚本不执行 所以要在脚本内调取特定函数
- 【转】MYSQL数据库四种索引类型的简单使用--MYSQL组合索引“最左前缀”原则
MYSQL数据库索引类型包括普通索引,唯一索引,主键索引与组合索引,这里对这些索引的做一些简单描述: (1)普通索引 这是最基本的MySQL数据库索引,它没有任何限制.它有以下几种创建方式: 创建索引 ...
- nano 命令 linux
用途说明 nano是一个字符终端的文本编辑器,有点像DOS下的editor程序.它比vi/vim要简单得多,比较适合Linux初学者使用.某些Linux发行版的默认编辑器就是nano.(nano - ...
- getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()区别详解
getDimension()是基于当前DisplayMetrics进行转换,获取指定资源id对应的尺寸.文档里并没说这里返回的就是像素,要注意这个函数的返回值是float,像素肯定是int. getD ...
- 从SQL查询分析器中读取EXCEL中的内容
很早以前就用sql查询分析器来操作过EXCEL文件了. 由于对于excel公式并不是很了解,所以很多时候处理excel中的内容,常常是用sql语句来处理的.[什么样的人有什么样的办法吧 :)] 今又要 ...
- (转)FFmpeg源代码简单分析:avformat_open_input()
目录(?)[+] ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结 ...
- Build opencv libraries for android arm, x86 ubuntu
废话不多说. 准备工作: 1. 下载源代码: http://opencv.org/ 编译平台:ubuntu14.04 opencv 2.4.6.1 本人用这样的办法编译了opecv 2.4.9 的没有 ...
- 炫酷霸气的HTML5/jQuery应用及源码
也许在5年前,HTML5还是一种很前卫的技术,大家还只是将它当做实验来看待,更别说产品应用了.但是现在HTML5已经非常流行,无论从PC端还是移动端,HTML5都扮演着非常重要的角色.今天我们要分享的 ...