0

我正在构建一个支持 Spring 的 MVC 4 应用程序。

我的 Web.config 如下所示

<configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
</configSections>

...

<spring>
    <context type="Spring.Context.Support.MvcApplicationContext, Spring.Web.Mvc3, Version=1.3.2.40943, Culture=neutral, PublicKeyToken=65e474d141e25e07">
      <!--<resource uri="config://spring/objects"/>-->
    </context>

    <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 type="Org.Zighinetto.MyNS.RepositoryHelper" singleton="true" id="Org.Zighinetto.RepositoryHelper">
        <property name="SessionFactory" ref="Org.Zighinetto.SessionFactory"/>
      </object>

      <object id="Org.Zighinetto.Ecommerce.NHibernateHelper" type="MvcTest.Utils.NHibernateHelper" singleton="true"/>
      <object id="Org.Zighinetto.Ecommerce.SessionFactory" type="NHibernate.ISessionFactory" factory-object="Org.Zighinetto.Ecommerce.NHibernateHelper" factory-method="CreateSessionFactory" />
    </objects>
</spring>

在我的控制器中,我想获得对该对象的引用,该对象基本上包装了所有 FNH DAO(我称它们为存储库......)

public CustomerController()
        {
            IApplicationContext ctx = new MvcApplicationContext();


            RepositoryHelper repoHelper = (RepositoryHelper)ctx.GetObject("Org.Zighinetto.RepositoryHelper");
            _customerRepository = repoHelper.CustomerRepository;
        }

GetObject调用崩溃并出现以下异常

No object named 'Org.Zighinetto.RepositoryHelper' is defined : Cannot find definition for object [Org.Zighinetto.RepositoryHelper]

[NoSuchObjectDefinitionException: No object named 'Org.Zighinetto.RepositoryHelper' is defined : Cannot find definition for object [Org.Zighinetto.RepositoryHelper]]
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:2065
   Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractObjectFactory.cs:1826
   Spring.Context.Support.AbstractApplicationContext.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:1538

我初始化 Spring.net 上下文是否不好?我该怎么做才能获得该对象引用?

[添加]

ctx.GetObjectDefinitionNames()返回空字符串。所以没有定义任何对象 Initializing AppContext with new MvcApplicationContext("~/Web.Config")doesn't change

4

1 回答 1

0

我固定使用IApplicationContext ctx = WebApplicationContext.GetRootContext()

于 2012-11-09T13:14:57.437 回答