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

如何配置Spring的XML文件及使用

2014-06-27 09:02 686 查看
App.config

<?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://Spring_bean.xml" />
</context>
<!--<objects xmlns="http://www.springframework.net" >
<object id="UserDao" type="SpringSample.UserDao, SpringSample"/>
<object id="UserService" type="SpringSample.UserService, SpringSample" >
<property name="UserDao" ref="UserDao"/>
</object>
</objects>-->
</spring>
</configuration>


Spring_bean.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" >
<object id="UserDao" type="SpringSample.UserDao, SpringSample"/>
<object id="UserService" type="SpringSample.UserService, SpringSample" >
<property name="UserDao" ref="UserDao"/>
</object>
</objects>


Program

IApplicationContext ctx = ContextRegistry.GetContext();
IUserService userService = ctx.GetObject("UserService") as IUserService;
Console.WriteLine(userService.GetName());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: