Spring.NET 依赖项未注入

发布于 2024-08-17 15:39:45 字数 1372 浏览 8 评论 0原文

我正在尝试创建一个 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 技术交流群。

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

发布评论

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

评论(3

垂暮老矣 2024-08-24 15:39:45

好吧,事实证明 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...)

朱染 2024-08-24 15:39:45

加载 spring 容器

ContextRegistry.getContext();

在引导类中,您必须通过指定 DepartmentDao 的程序集名称的方式

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

in your bootstrapclass you have to load the spring container

ContextRegistry.getContext();

by the way you need to specify the assembly name for DepartmentDao

<object id="DepartmentDao" type="App.Data.DepartmentDao, App" />
友欢 2024-08-24 15:39:45

更多信息:我还有 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);
    }

Further information: I also have classes for SpringApplicationController and 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();
        }
    } 
}

I reference this in my Global.asax as follows:

    protected void Application_Start()
    {

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


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