首页 > 代码库 > 如何配置Spring的XML文件及使用
如何配置Spring的XML文件及使用
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());
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。