无法从MVC应用程序创建Spring.net对象

时间:2012-11-09 12:45:48

标签: asp.net-mvc spring.net applicationcontext

我正在使用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>

在我的Controller中,我希望得到一个对象,它基本上包含了所有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()返回空字符串。所以没有定义任何对象 使用new MvcApplicationContext("~/Web.Config")初始化AppContext不会更改

1 个答案:

答案 0 :(得分:0)

我使用IApplicationContext ctx = WebApplicationContext.GetRootContext()

修复了