Spring.NET学习日记-2

配置及对象初始化

 

一,配置文件进行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]

1
2
3
4
5
6
7
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>
复制代码

 

这里<context>中的type是可选的

通过构造器创建对象

<object id="exampleObject"   type="Examples.ExampleObject, ExamplesLibrary"/>

type属性的格式:类型的全名,然后是一个逗号,最后是类型所在的程序集名称。

 

如果需要为嵌套类型创建对象,可以使用+号。例如,如果在类型Examples.ExampleObject嵌套定义了类型Person

<object id="exampleObject" type="Examples.ExampleObject+Person, ExamplesLibrary"/>

 

通过静态工厂方法创建对象

下面的对象就是通过静态工厂方法创建的。注意:对象定义中的type并非是要创建的对象的类型,而是包含了工厂方法的类型;同时,CreateInstance必须是静态方法。 

<object id="exampleObject"  type="Examples.ExampleObjectFactory, ExamplesLibrary"  factory-method="CreateInstance"/>

通过实例工厂方法创建对象

如果要通过实例工厂方法创建对象,对象定义就不能包含type属性,而要用factory-object属性引用工厂方法所在的对象;注意,该属性值必须是包含工厂方法的对象的名称,且该对象必须定义在当前容器或父容器中。工厂方法的方法名则通过factory-method属性指定。

<!-- the factory object, which contains an instance method called 'CreateInstance' -->
<object id="exampleFactory" type="..."/>
<!-- the object that is to be created by the factory object -->
<object id="exampleObject"
      factory-method="CreateInstance"
      factory-object="exampleFactory"/>



 

posted @ 2014-10-08 11:35  一生的爱恋  阅读(74)  评论(0)    收藏  举报