码迷,mamicode.com
首页 > 编程语言 > 详细

Spring-----配置及对象初始化(1)

时间:2014-06-26 23:23:21      阅读:526      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   http   

一,配置文件进行Spring初始化

1,配置文件编写

<?xml version="1.0" encoding="utf-8" ?> <configuration>
/************* 这里会报异常“Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常。把配置注释掉就行了 <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup>
***************/ <configSections>
//初始化Spring <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配置节点 <spring> <context> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net"> <description>人类</description> <object id="Model" type="Model.Person, Model" /> </objects> </spring> </configuration>

 

 

2,初始化代码

异常:No object named ‘Person‘ is defined : Cannot find definition for object [Person]

 static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();

            IPerson dao = ctx.GetObject("Person") as IPerson;

        }

  

这里报异常是因为在配置文件中没有找到  <object id="Person" type="Model.Person, Model" />  的节点

 

 

需在项目中增加Spring.Core.dll的引用

 

二,使用Xml文件Spring初始化

(1)具体文件的初始化

   编写Objects.xml

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="Person" type="Model.Person, Model">

  </object>

</objects>

  后台初始化

        private static void ReadFromXml()
        {
            IResource input = new FileSystemResource("Objects.xml");  //路径----这里使用的是相对路径,也可以使用绝对路径,如果路径错了会报异常

            IObjectFactory factory = new XmlObjectFactory(input);

            IPerson person = factory.GetObject("Person") as IPerson;

       person.Speak("hello"); }

 

(2)程序集下寻找配置文件

  private static void ReadFromMuiltDoc()
        {
            //需满足URI语法。
            //file://文件名
            //assembly://程序集名/命名空名/文件名
            string[] xmlFiles = new string[] 
            {
                "file://Objects.xml"
            };

            IApplicationContext context = new XmlApplicationContext(xmlFiles);

            IObjectFactory factory = (IObjectFactory)context;

            IPerson person = factory.GetObject("Person") as IPerson;

            person.Speak("多文件初始化");
        }

这种方式相对灵活

(3)在配置文件App.config或Web.config添加自定义配置节点

在配置文件中要引入<objects xmlns="http://www.springframework.net"/>命名空间,否则程序将会无法实例化Spring.NET容器。

 

<?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="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/>
      <resource uri="config://spring/objects" />
    </context>
    <objects xmlns="http://www.springframework.net"/> <!--必要-->
  </spring>

 

 

 

Spring-----配置及对象初始化(1),布布扣,bubuko.com

Spring-----配置及对象初始化(1)

标签:des   style   class   blog   code   http   

原文地址:http://www.cnblogs.com/anbylau2130/p/3807743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!