Spring.NET依赖关系没有注入

时间:2010-01-11 18:13:33

标签: c# asp.net asp.net-mvc 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>

我更喜欢使用Property注入而不是构造函数,但目前这两种方法都不起作用。

3 个答案:

答案 0 :(得分:3)

嗯,事实证明,ASP.NET MVC和Spring.NET不相处......

但是,MvcContrib软件包(实际上是Extras软件包)似乎已经解决了这个问题。该软件包有一个有效的Spring Controller工厂实现,一切都很愉快。

(有点让我想起试图让Struts 1.X和Spring在Java方面工作......)

答案 1 :(得分:1)

在你的bootstrapclass中你必须加载弹簧容器

ContextRegistry.getContext();

您需要指定DepartmentDao的程序集名称

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

答案 2 :(得分:0)

更多信息:我还有SpringApplicationController和SpringControllerFactory的类:

SpringApplicationController.cs:

public static class SpringApplicationContext
{
    private static IApplicationContext Context { get; set; }

    /// <summary> 
    /// Returns a boolean value if the current application context contains an named object. 
    /// </summary> 
    /// <param name="objectName">Accepts the name of the object to check.</param> 
    public static bool Contains(string objectName)
    {
        SpringApplicationContext.EnsureContext();
        return SpringApplicationContext.Context.ContainsObject(objectName);
    }

    /// <summary> 
    /// Return a instance of an object in the context by the specified name. 
    /// </summary> 
    /// <param name="objectName">Accepts a string object name.</param> 
    public static object Resolve(string objectName)
    {
        SpringApplicationContext.EnsureContext();
        return SpringApplicationContext.Context.GetObject(objectName);
    }

    /// <summary> 
    /// Return a instance of an object in the context by the specified name and type. 
    /// </summary> 
    /// <typeparam name="T">Accepts the type of the object to resolve.</typeparam> 
    /// <param name="objectName">Accepts a string object name.</param> 
    public static T Resolve<T>(string objectName)
    {
        return (T)SpringApplicationContext.Resolve(objectName);
    }

    private static void EnsureContext()
    {
        if (SpringApplicationContext.Context == null)
        {
            SpringApplicationContext.Context = ContextRegistry.GetContext();
        }
    }
} 

SpringControllerFactory.cs:

public class SpringControllerFactory : DefaultControllerFactory
{
    public IController CreateController(RequestContext context, Type controllerType)
    {
        IResource input = new FileSystemResource(context.HttpContext.Request.MapPath("Resource\\objects.xml"));
        IObjectFactory factory = new XmlObjectFactory(input);

        return (IController) factory.GetObject(controllerType.Name);
    }

    public IController CreateController(RequestContext context, string controllerName)
    {
        IController controller = null;
        string controllerClassName = string.Format("{0}Controller", controllerName);

        if (SpringApplicationContext.Contains(controllerClassName))
        {
            controller = SpringApplicationContext.Resolve<IController>(controllerClassName);
            this.RequestContext = context;
        }
        else
        {
            controller = base.CreateController(context, controllerName);
        }
        return controller; 
    }

    public override void ReleaseController(IController controller)
    {
        IDisposable disposable = controller as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    } 
}

我在Global.asax中引用它如下:

    protected void Application_Start()
    {

        ControllerBuilder.Current.SetControllerFactory(typeof(App.Util.SpringControllerFactory));


        RegisterRoutes(RouteTable.Routes);
    }