将对象的新实例注入到每个请求处理程序中 [英] Inject a new instance of an object into each request handler

查看:91
本文介绍了将对象的新实例注入到每个请求处理程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多Spring RestControllers,它们的方法都用RequestMapping注释.我现在想将自定义对象注入这些RequestMapping方法中,并为每个请求创建一个自定义实例.

I have a lot of Spring RestControllers with methods annotated with RequestMapping. I now would like to inject a custom object into these RequestMapping methods, and create an custom instance for each request.

我想写类似下面的东西:

I would like to write something like the following:

@RequestMapping("/search")
public SomeReturnObject foobar(@RequestParam("query") String query, MyRequestFoo foo) {
   // ...
}

现在,我想创建一种机制,其中对该方法的每个调用(即每个请求)都将获得一个创建的MyRequestFoo的新实例,并将其注入该方法中.如果使用参数注解而不是按类型注入更好,那么也可以(例如@MyRequestInject MyRequestFoo foo).

Now I would like to create a mechanism, where each call to that method (i.e. each request) get a new instance of MyRequestFoo created and injected into the method. If this would work better with an parameter annotation instead of injecting by type, that would also be okay (e.g. @MyRequestInject MyRequestFoo foo).

我需要知道是否现在可以创建一个方法来创建MyRequestFoo的新实例,特别是针对该请求,如下所示:

I need to know if I can create now a method that creates a new instance of MyRequestFoo especially for that request, like the following:

public MyRequestFoo createRequestInstanceSomehow(HttpServletRequest request) {
   // extract some values from the HttpServletRequest and create a
   // new MyRequestFoo instance from that and return it
}

是否有可能通过任何方式创建这种机制,以便将自定义的每个请求对象注入到我的请求处理方法中?

Is this possible by any means to create such a mechanism, so that I can inject custom per request objects into my request handling methods?

推荐答案

Spring MVC具有参数解析程序构造,该构造直接支持您的请求.每个用 @RequestMapping 注释的处理程序方法都将进行参数解析,其中框架将扫描处理程序参数,检查类型并实例化适当的对象.这就是注入请求,模型和许多其他类型的背后机制,仅通过在处理程序的方法签名中声明对象即可.

Spring MVC has a arguments resolver construct that directly supports your request. Every handler method annotated with @RequestMapping will be subject to argument resolving, where the framework scans through the handler arguments, checks the type and instantiates an appropriate object. That is the mechanism behind injecting request, model and a number of other types, just by declaring the object in the handler's method signature.

您可以编写一个自定义参数解析器,以解析自定义类型并在方法中可用.程序是简单的三步过程

You can write a custom argument resolver to have the custom types resolved and available in the method. The procedure is simple three step process

  1. 创建POJO类,在您的情况下为 MyRequestFoo

创建解析器,例如

  public class MyRequestFooResolver implements HandlerMethodArgumentResolver {

        @Override
        public boolean supportsParameter(MethodParameter parameter) {

            return parameter.getParameterType().equals(MyRequestFoo.class);
        }

        @Override
        public Object resolveArgument(MethodParameter parameter, 
                                      ModelAndViewContainer mavContainer,
                                      NativeWebRequest webRequest, 
                                      WebDataBinderFactory binderFactory)
        throws Exception {

            return new MyRequestFoo();
        }
    }

3.注册解析器

 <mvc:annotation-driven>
     <mvc:argument-resolvers>
         <bean class="your.package.MyRequestFooResolver "></bean>  
     </mvc:argument-resolvers>
 </mvc:annotation-driven>

或在Java配置中

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addArgumentResolvers(List< Handlermethodargumentresolver > argumentResolvers) {
        MyRequestFooResolver myRequestFooResolver = new MyRequestFooResolver ();
        argumentResolvers.add(myRequestFooResolver );
  }
}

比起仅通过将类型添加为处理程序方法参数来使用它

Than you use it just by adding the type as a handler method argument

@RequestMapping("/search")
public SomeReturnObject search(MyRequestFoo foo) {
   // ...
}

这篇关于将对象的新实例注入到每个请求处理程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆