Spring.NET 依赖项未注入
我正在尝试创建一个 ASP.NET MVC 应用程序,使用 Spring.NET 注入依赖项。该应用程序具有三层:控制器、服务和数据。
我已经在文件“~\Resources\objects.xml”中定义了对象。
我的第一个对象 UserAccountController 需要注入两个服务层类:UserAccountService 和 DepartmentService。因此,objects.xml 中的定义如下所示:
<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 包含以下内容:
<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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,事实证明 ASP.NET MVC 和 Spring.NET 无法相处……
但是,MvcContrib 包(实际上是 Extras 包)似乎已经解决了这个问题。该包有一个可以工作的 Spring Controller 工厂实现,一切都很顺利。
(有点让我想起尝试让 Struts 1.X 和 Spring 在 Java 端工作......)
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...)
加载 spring 容器
在引导类中,您必须通过指定 DepartmentDao 的程序集名称的方式
in your bootstrapclass you have to load the spring container
by the way you need to specify the assembly name for DepartmentDao
更多信息:我还有 SpringApplicationController 和 SpringControllerFactory 的类:
SpringApplicationController.cs:
SpringControllerFactory.cs:
我在 Global.asax 中引用它,如下所示:
Further information: I also have classes for SpringApplicationController and SpringControllerFactory:
SpringApplicationController.cs:
SpringControllerFactory.cs:
I reference this in my Global.asax as follows: