您的位置:首页 > 编程语言 > Java开发

Spring.Net 配置文件

2015-06-02 09:25 387 查看
方法一. 直接在程序配置文件中配置

<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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: