0

I am working with and mvc4 web application and using spring.net v2 M2 for the dependency injection.

I am wondering is possible to inject in to my controllers, filters/attributes on to the controller?

Andreas
  • 5,251
  • 30
  • 43
amateur
  • 43,371
  • 65
  • 192
  • 320

1 Answers1

1

Same as in Asp.Net MVC3: Register a custom spring aware FilterAttributeFilterProvider derive your application from SpringMvcApplication instead of HttpApplication and see the magic work. Sample impl. not production ready:

public class FilterProvider : FilterAttributeFilterProvider, IApplicationContextAware
{
    public IApplicationContext ApplicationContext
    {
        set;
        get;
    }

    public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(controllerContext, actionDescriptor);
        foreach (var filter in filters)
        {
            ApplicationContext.ConfigureObject(filter.Instance, filter.Instance.GetType().Name);
            yield return filter;
        }
    }
}
Andreas
  • 5,251
  • 30
  • 43
  • Thanks for this information - is it possible to use this to only inject in to specific controllers and not all? – amateur Jan 10 '13 at 10:14
  • The code does property injection into ActionFilter objects. Instantiation and resolving mvc controller is supported out of the spring.net box, imho spring raises an exception when the controller is not found in the config; however you could fallback to the default MVC behavior (Activator.CreateInstance). – Andreas Jan 10 '13 at 16:35