使用 Spring 进行 ASP.NET MVC Global.asax 注入

发布于 2024-11-27 19:09:51 字数 1367 浏览 0 评论 0原文

我正在尝试将属性注入到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2024-12-04 19:09:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文