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

1.spring环境的搭建

时间:2015-03-21 11:03:42      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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

</configuration>
2.根据app.config设置的object.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="PersonDao" type="FirstSpringNetApp.PersonDao, FirstSpringNetApp" />
 
</objects>

3.persondao类

namespace FirstSpringNetApp
{
    public class PersonDao
    {
        public override string ToString()
        {
            return "我是PersonDao";
        }
    }
}

4.对象的获取方式的说明 通常我们使用程序集路径

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
using Spring.Core.IO;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Xml;

namespace FirstSpringNetApp
{
    class Program
    {
        static void Main(string[] args)
        {
            AppRegistry();
            XmlSystem();
            Console.ReadLine();
        }

        static void AppRegistry()
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            Console.WriteLine(ctx.GetObject("PersonDao").ToString());
        }

        static void XmlSystem()
        {
            string[] xmlFiles = new string[]
            {
                //"file://Objects.xml"  //, 文件名
                "assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"  //程序集
            };
            IApplicationContext context = new XmlApplicationContext(xmlFiles);

            IObjectFactory factory = (IObjectFactory)context;
            Console.WriteLine(factory.GetObject("PersonDao").ToString());
        }

        static void FileSystem()
        {
            IResource input = new FileSystemResource(@"D:\Objects.xml");  //实际物理路径
            IObjectFactory factory = new XmlObjectFactory(input);
            Console.WriteLine(factory.GetObject("PersonDao").ToString());
        }
    }
}

1.spring环境的搭建

标签:

原文地址:http://www.cnblogs.com/kexb/p/4355170.html

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