Spring.Net 配置文件

时间:2023-03-08 21:51:28

方法一. 直接在程序配置文件中配置

 <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"/>
</context>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="PhoneNumberWhere.Person, PhoneNumberWhere"> //类的全名称,程序集名称
<property name="Name" value="MyName"/>     //简单属性配置,key-value
<property name="SonMan" ref="Son"/>      //复杂属性配置,ref
</object>
<object id="Son" type="PhoneNumberWhere.Son, PhoneNumberWhere"> //
</object>
</objects>
</spring> </configuration>

方法二. 引用外部xml文件配置

<?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 uri="file://objs.xml"/> //引用xml文件,并把xml设置为【始终复制】嵌入的资源
</context>
<objects xmlns="http://www.springframework.net">
</objects>
</spring>
</configuration>

objs.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="PhoneNumberWhere.Person, PhoneNumberWhere">
<property name="Name" value="MyName"/>
<property name="SonMan" ref="Son"/>
</object>
<object id="Son" type="PhoneNumberWhere.Son, PhoneNumberWhere">
<constructor-arg index="0" value="son.txt"/>
</object>
</objects>

创建对象【引用  Spring.Core.dlll   Common.Logging.dll】

IApplicationContext ctx = ContextRegistry.GetContext();
Person person = (Person)ctx.GetObject("Person");