使用 Spring 进行 ASP.NET MVC Global.asax 注入
我正在尝试将属性注入到我的 SpringMvcApplication 中(来自 Spring. Web.Mvc)。
public class MvcApplication : SpringMvcApplication
{
public ISomeService SomeService { get; set; }
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// Use SomeService
}
}
然后我有一个 Services.xml 文件,其中包含我的定义
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="HttpApplicationConfigurer" type="Spring.Context.Support.HttpApplicationConfigurer, Spring.Web">
<property name="ApplicationTemplate">
<object>
<property name="SomeService" ref="ISomeService" />
</object>
</property>
</object>
<object id="ISomeService" type="WebProject.Services.SomeService, WebProject">
<constructor-arg ref="UserService" />
</object>
</objects>
每当我尝试使用 SomeService 属性时,我都会收到空引用异常。是我的配置错误吗?我需要任何额外的配置吗?
我现在的解决方法似乎不合适是这样的
protected override void ConfigureApplicationContext()
{
base.ConfigureApplicationContext();
SomeService = (ISomeService)ContextRegistry.GetContext().GetObject("ISomeService");
}
I'm trying to inject a property into my SpringMvcApplication (from Spring.Web.Mvc).
public class MvcApplication : SpringMvcApplication
{
public ISomeService SomeService { get; set; }
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// Use SomeService
}
}
Then I have a Services.xml file which contains my definitions
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="HttpApplicationConfigurer" type="Spring.Context.Support.HttpApplicationConfigurer, Spring.Web">
<property name="ApplicationTemplate">
<object>
<property name="SomeService" ref="ISomeService" />
</object>
</property>
</object>
<object id="ISomeService" type="WebProject.Services.SomeService, WebProject">
<constructor-arg ref="UserService" />
</object>
</objects>
Whenever I try to use the SomeService property, I get a null reference exception. Is my configuration wrong? Do I need any additional configuration?
My workaround for now that doesn't seem proper is this
protected override void ConfigureApplicationContext()
{
base.ConfigureApplicationContext();
SomeService = (ISomeService)ContextRegistry.GetContext().GetObject("ISomeService");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MvcApplication 扩展了 SpringMvcApplication 类,该类实际上设置了 Spring.net 上下文,因此对我来说,使用 di 来配置其属性看起来有点奇怪。
您现在重写的方法旨在允许您在 Spring.net 应用程序上下文传递到 asp.net mvc 基础结构之前对它进行最后一秒的更改。
在您的情况下,一个稍微更好的解决方法是让 SomeService 直接在其 getter 中访问上下文。这样您就可以利用 Spring 托管对象范围的优势,而这是您现在所没有的。
MvcApplication extends the SpringMvcApplication class, which actually sets up the Spring.net context, so to me it looks kind of strange to use di to configure its properties.
The method you are overriding now is intended to allow you to make last second changes to the Spring.net application context, just before it is passed to the asp.net mvc infrastructure.
In your case, a slightly better workaround would be to have SomeService access the context directly in its getter. This way you'd have the advantage of Spring managed object scopes, which you don't have now.