方法一. 直接在程序配置文件中配置
1 <configuration> 2 3 <configSections> 4 <sectionGroup name="spring"> 5 <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> 6 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 7 </sectionGroup> 8 </configSections> 9 10 <spring> 11 <context> 12 <resource uri="config://spring/objects"/> 13 </context> 14 <objects xmlns="http://www.springframework.net"> 15 <object id="Person" type="PhoneNumberWhere.Person, PhoneNumberWhere"> //类的全名称,程序集名称 16 <property name="Name" value="MyName"/> //简单属性配置,key-value 17 <property name="SonMan" ref="Son"/> //复杂属性配置,ref 18 </object> 19 <object id="Son" type="PhoneNumberWhere.Son, PhoneNumberWhere"> // 20 </object> 21 </objects> 22 </spring> 23 24 </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");
时间: 2024-11-06 17:03:37