App.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 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 <spring> 10 <context> 11 <!--<resource uri="config://spring/objects" />--> 12 <resource uri="file://Spring_bean.xml" /> 13 </context> 14 <!--<objects xmlns="http://www.springframework.net" > 15 <object id="UserDao" type="SpringSample.UserDao, SpringSample"/> 16 <object id="UserService" type="SpringSample.UserService, SpringSample" > 17 <property name="UserDao" ref="UserDao"/> 18 </object> 19 </objects>--> 20 </spring> 21 </configuration>
Spring_bean.xml
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net" > 3 <object id="UserDao" type="SpringSample.UserDao, SpringSample"/> 4 <object id="UserService" type="SpringSample.UserService, SpringSample" > 5 <property name="UserDao" ref="UserDao"/> 6 </object> 7 </objects>
Program
1 IApplicationContext ctx = ContextRegistry.GetContext(); 2 IUserService userService = ctx.GetObject("UserService") as IUserService; 3 Console.WriteLine(userService.GetName());