开发者

ASP.NET MVC Global.asax Injection with Spring

开发者 https://www.devze.com 2023-03-24 11:18 出处:网络
I\'m trying to inject a property into开发者_JS百科 my SpringMvcApplication (from Spring.Web.Mvc).

I'm trying to inject a property into开发者_JS百科 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");
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号