022. 处理No object named '*' is defined : Cannot find definition for object [*]错误和Cannot find matching factory method '*on Type [*]

第一个错误

报错信息:

“/”应用程序中的服务器错误。
No object named 'DbSessionFactory' is defined : Cannot find definition for object [DbSessionFactory]
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: Spring.Objects.Factory.NoSuchObjectDefinitionException: No object named 'DbSessionFactory' is defined : Cannot find definition for object [DbSessionFactory]

源错误:

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

配置文件:

dals.xml配置文件内容:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object name="Dbsession" type="muTian.SysAdmin.DALFactory.DbSession , muTian.SysAdmin.DALFactory"
          singleton="false"  factory-method="GetCurrentDbSession" factory-object="DbSessionFactory"></object>
</objects>

web.config配置文件主要内容:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,  Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
      <!--<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="file://~/Config/dals.xml" />
      <resource uri="file://~/Config/service.xml" />
      <resource uri="file://~/Config/controllers.xml" />
    </context>
  </spring>
  <connectionStrings>

错误分析:

由于在dals.xml文件中引用了factory-object="DbSessionFactory" , 但是DbSessionFactory并未定义, 所以就会报出未定义的错误, 增加一项配置即可

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object name="DbSessionFactory" type="muTian.SysAdmin.DALFactory.DbSessionFactory, muTian.SysAdmin.DALFactory"
        singleton="true">
    <!--工廠可以使用單例-->
  </object>
  <object name="DbSession" type="muTian.SysAdmin.DALFactory.DbSession, muTian.SysAdmin.DALFactory"
          singleton="false"  factory-method="GetCurrentDbSession" factory-object="DbSessionFactory"></object>
</objects>

----------------------------------------------------------------------------------------------------------------

第二个错误:

报错信息:

“/”应用程序中的服务器错误。
Cannot find matching factory method 'GetCurrentDbSession on Type [muTian.SysAdmin.DALFactory.DbSessionFactory].
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: Spring.Objects.Factory.ObjectDefinitionStoreException: Cannot find matching factory method 'GetCurrentDbSession on Type [muTian.SysAdmin.DALFactory.DbSessionFactory].

源错误:

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

解决思路:

 首先看下GetCurrentDbSession方法, 发现它是一个静态的方法, 静态方法是在创建该类型的实例的时候, 才会注入. 容器创建了一个此方法对应的类的一个实例的时候, 才会注入. 但是由于它是个静态的, 不需要拥有类的实例就可以调用, 当直接调用的时候, 容器还没有创建实例. 所以只能得到null值.

解决方案:

可以将GetCurrentDbSession的static去掉. 也可以将对应的配置设置为 singleton="true"

 

 

posted on 2017-03-02 08:40  印子  阅读(1927)  评论(0)    收藏  举报

导航