1.spring环境的搭建

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());
        }
    }
}

有时出现spring调用目标异常却束手无策,这里给出一些可能性

1.没有引用相应的dll 如 common.logging.dll common.logging.log4net.dll ,log4net.dll ,spring.core.dll,spring.data.dll,spring.data.nhibernate30.dll ,spring.web.dll,spring.service.dll,Spring.Aop.dll,Spring.Scheduling.Quartz.dll,Quartz.dll,nhibernate.dll

除了这些基本的dll在跟hibernate继承的时候,要引用相应的数据客户端dll,我这里用oralce,所以引用oracle.dataaccess.dll(64位或者32位,自己看情况)

2.如果是控制台项目或者winform项目相应使用spring注入的context,如我这里是

<context>
<resource uri="file://Config/BLL.xml" />
<resource uri="file://Config/Dao2.xml" />
<resource uri="file://Config/Service.xml" />
</context>

把他们改为始终复制 内容

3.用了64为的oralcle.dataaccess.dll  项目也要编译为64位的dll,把解决方案->属性->配置属性里相应项目的改为x64,配置管理器找到相应项目也改成x64,还有如我的类库是EtlTranslateInterface,那么选中它,右键属性->目标平台->x64

4.新建项目的app.config里如果有startup标签页删除掉

5.你的项目的spring和hibernate等相应的版本是匹配的可运行的,如spring.core.dll用1.0的hibernate.dll用2.0可能也不行,这里打个比方,我没看他们的版本号

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