【问题标题】:Inject specific beans as parameters in function with multiple parameters在具有多个参数的函数中注入特定的bean作为参数
【发布时间】:2014-12-04 23:06:49
【问题描述】:

我知道我可以使用@Autowired 在一个类中注入一个 bean。

现在我很好奇:: 我不想拥有@Autowired 的私有属性。 我的控制器中有一个函数,我想直接在函数中注入 bean 作为参数。 我收到一条错误消息,说文件和令牌不是 bean。

有没有办法自动装配或只注入我需要的 bean 作为参数?

@Controller
public class SpinalToolboxWebController {

    /*@Autowired
    private FileOperationsService fileOperationsService;


    @Autowired
    private Comparator<String> comparator;

    @Autowired
    private ServerResponse serverResponse;

    @Autowired
    private SoftwareCommunicationService softwareCommunicationService;

    @Autowired
    private StringBuffer stringBuffer;

    @Autowired
    private UserEnvironmentService userEnvironmentService;*/


    @Autowired
    @RequestMapping(value="/upload", method = RequestMethod.POST, produces="application/json")
    public  @ResponseBody
    ServerResponse handleUploadedFiles(@RequestParam(value = "file") MultipartFile file,
                                       @RequestParam(value="token") String token, 
                                       SoftwareCommunicationService softwareCommunicationService,
                                       FileOperationsService fileOperationsService, 
                                       ServerResponse serverResponse )throws IOException {

        System.out.println("Passing throught upload controller");

        if(!fileOperationsService.isUploadedFileExtensionAllowed(file.getOriginalFilename()))
        {
            serverResponse.setUndefinedResponse();
            return serverResponse;
        }

        if(fileOperationsService.uploadFile(file, token)){
            serverResponse.setResponse(file, softwareCommunicationService.generateRawAndHeader(file));
        }
        else{
            serverResponse.setUndefinedResponse();
        }
        return serverResponse;
    }

}

【问题讨论】:

  • 你为什么不想要字段?

标签: java spring spring-mvc


【解决方案1】:

作为内置功能,没有。你不能按照你的建议去做。

然而,Spring 提供了您自己编程此功能的工具。您需要提出一个标记注释类型。像@MethodBean 这样的东西。您可以注释您想要从ApplicationContext 注入的处理程序方法参数。然后,您需要编写一个扩展 HandlerMethodArgumentResolver 并查找此注释的类。您必须添加一个 @Autowired WebApplicationContext 字段,从中获取 bean 并将它们提供给方法。

然后您将该 bean 注册为我们 MVC 堆栈的 HandlerMethodArgumentResolvers 的一部分。

当 Spring 确定它必须调用示例中的处理程序方法时,现在看起来像这样

@RequestMapping(value="/upload", method = RequestMethod.POST, produces="application/json")
public @ResponseBody
ServerResponse handleUploadedFiles(@RequestParam(value = "file") MultipartFile file,
                                   @RequestParam(value="token") String token, 
                                   @MethodBean SoftwareCommunicationService softwareCommunicationService,
                                   @MethodBean FileOperationsService fileOperationsService, 
                                   @MethodBean ServerResponse serverResponse )throws IOException {

它将使用适当的HandlerMethodArgumentResolver 来解析每个参数的参数。

对于@MethodBean注解的参数,它会找到你的自定义实现,在注入的WebApplicationContext中查找参数类型的bean,并将其作为参数提供。

【讨论】:

  • 感谢您的精彩解释。我想到这一点的原因是,由于我的控制器是单例的,如果我使用“在 Autowired private ServerResponse serverResponse”例如,然后在方法中使用 serverResponse,是否存在用户访问另一个人的数据的风​​险?
  • @bigjel 这取决于ServerResponse 是什么。你的意思是ServletResponse?如果是,那么没有问题。 Spring 会做一些智能处理,这样你的控制器就只能看到它当前正在使用的ServletResponse
  • 不,这是我为服务器响应创建的一个类。它包含私有字符串字段...和 ​​get/set 方法。
  • @bigjel 你打算用它做豆子吗?它是请求范围还是单例范围?这些是您在考虑并发问题时应该回答的问题。
  • @bigjel 是的,一个请求 - 一个请求范围的 bean。如果你使用@Autowired 字段,Spring 会注入一个知道如何检索请求范围 bean 的 Proxy
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多