Search code examples
c#asp.netasp.net-mvcspring.net

Spring.NET Dependencies Not Injecting


I am trying to create an ASP.NET MVC application, using Spring.NET to inject dependencies. The application has three tiers: Controller, Service, and Data.

I have defined the objects in the file "~\Resources\objects.xml".

My first object, UserAccountController, requires the injecion of two Service-tier classes: UserAccountService and DepartmentService. So, the definition in objects.xml looks like this:

<object id="UserAccountController" type="App.Controllers.UserAccountController, App">
    <constructor-arg index="0" ref="DepartmentService" />
    <constructor-arg index="1" ref="UserAccountService" />
</object>

<object id="UserAccountService" type="App.Service.UserAccountService, App">
    <property name="UserAccountDao" ref="UserAccountDao" />
</object>

<object id="UserAccountDao" type="App.Data.UserAccountDao, App" />

<object id="DepartmentService" type="App.Service.DepartmentService, App">
    <property name="DepartmentDao" ref="DepartmentDao" />
</object>

<object id="DepartmentDao" type="App.Data.DepartmentDao" />

Webconfig contains this:

<sectionGroup name="spring">
        <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
    </sectionGroup>
</configSections>
<spring>
    <context>
        <resource uri="~/Resources/objects.xml" />
    </context>
</spring>

I would prefer to use Property injection rather than constructor, but currently neither method is working.


Solution

  • Well, it turned out to be that ASP.NET MVC and Spring.NET just don't get along...

    However, the MvcContrib package (actually, the Extras package) seems to have solved the issue. The package had a Spring Controller factory implementation that worked, and everything was happy.

    (Kind of reminds me of trying to make Struts 1.X and Spring work on the Java side...)