Is it possible to dynamically set RequestMappings in Spring MVC?

后端 未结 6 1793
心在旅途
心在旅途 2020-12-04 13:31

I\'ve been using Spring MVC for three months now. I was considering a good way to dynamically add RequestMapping. This comes from the necessity to put controller parts in a

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 14:23

    I know this is really old but I figured I toss this in in case anyone else has the same rough experience I did trying to make this work. I ended up taking advantage of two features of Spring: the ability to dynamically register beans after the context is started and the afterPropertiesSet() method on the RequestMappingHandlerMapping object.

    When RequestMappingHandlerMapping is initialized, it scans the context and creates a map of all @RequestMappings that it needs to serve (presumably for performance reasons). If you dynamically register beans annotated with @Controller, they will not be picked them up. To retrigger this scan, you just need to call afterPropertiesSet() after you've added your beans.

    In my particular use case, I instantiated the new @Controller objects in a separate Spring context and needed to wire them into my WebMvc context. The particulars of how the objects don't matter for this though, all you need is an object reference:

    //register all @Controller beans from separateContext into webappContext
    separateContext.getBeansWithAnnotation(Controller.class)
       .forEach((k, v) -> webappContext.getBeanFactory().registerSingleton(k, v));
    
    //find all RequestMappingHandlerMappings in webappContext and refresh them
    webappContext.getBeansOfType(RequestMappingHandlerMapping.class)
       .forEach((k, v) -> v.afterPropertiesSet());
    

    For example, you could also do this:

    //class annotated with @Controller
    MyController controller = new MyController
    
    //register new controller object
    webappContext.getBeanFactory().registerSingleton("myController", controller);
    
    //find all RequestMappingHandlerMappings in webappContext and refresh them
    webappContext.getBeansOfType(RequestMappingHandlerMapping.class)
       .forEach((k, v) -> v.afterPropertiesSet());
    

提交回复
热议问题