Java 使用多个参数将特定bean作为参数注入函数中
我知道我可以使用@Autowired在类中注入bean 现在我开始好奇了: 我不想在@Autowired中使用私有属性。 我的控制器中有一个函数,我想直接在函数中注入bean作为参数。 我得到一个错误,说文件和令牌不是bean 有没有一种方法可以自动连接或注入我需要的bean作为参数Java 使用多个参数将特定bean作为参数注入函数中,java,spring,spring-mvc,Java,Spring,Spring Mvc,我知道我可以使用@Autowired在类中注入bean 现在我开始好奇了: 我不想在@Autowired中使用私有属性。 我的控制器中有一个函数,我想直接在函数中注入bean作为参数。 我得到一个错误,说文件和令牌不是bean 有没有一种方法可以自动连接或注入我需要的bean作为参数 @Controller public class SpinalToolboxWebController { /*@Autowired private FileOperationsService f
@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;
}
}
@控制器
公共类SpinalToolboxWebController{
/*@自动连线
私人文件操作服务文件操作服务;
@自动连线
专用比较器;
@自动连线
专用服务器响应服务器响应;
@自动连线
私有软件通信服务软件通信服务;
@自动连线
私有字符串缓冲区字符串缓冲区;
@自动连线
私有用户环境服务用户环境服务*/
@自动连线
@RequestMapping(value=“/upload”,method=RequestMethod.POST,products=“application/json”)
公共@ResponseBody
ServerResponse handleUploadedFiles(@RequestParam(value=“file”)多部分文件,
@RequestParam(value=“token”)字符串标记,
软件通信服务软件通信服务,
文件操作服务文件操作服务,
ServerResponse(服务器响应)引发IOException{
System.out.println(“通过上传控制器”);
如果(!fileOperationsService.isUploadedFileExtensionAllowed(file.getOriginalFilename()))
{
serverResponse.setUndefinedResponse();
返回服务器响应;
}
if(fileOperationsService.uploadFile(文件,令牌)){
setResponse(文件,softwareCommunicationService.GenerateRandHeader(文件));
}
否则{
serverResponse.setUndefinedResponse();
}
返回服务器响应;
}
}
作为一项内置功能,不可以。您不能按建议执行
然而,Spring提供了自己编写此功能的工具。您需要提出一种标记注释类型。类似于@MethodBean
的东西。您可以从ApplicationContext
注释要注入的处理程序方法参数。然后,您需要编写一个类来扩展HandlerMethodArgumentResolver
,并查找此注释。您必须添加一个@Autowired
WebApplicationContext
字段,从中获取bean并将其提供给方法
然后将此bean注册为MVC堆栈的HandlerMethodArgumentResolver
s的一部分
当Spring确定必须调用示例中的handler方法时,它现在如下所示
@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,并将其作为参数提供 你为什么不想要字段?谢谢你的解释。我考虑这一点的原因是,由于我的控制器是一个单例控制器,如果我使用“at Autowired private ServerResponse ServerResponse”,然后在方法中使用ServerResponse,是否存在用户访问另一个数据的风险?@bigjel,这取决于ServerResponse
是什么。你的意思是ServletResponse
?如果是,则没有问题。Spring将进行一些智能处理,使您的控制器只能看到它当前使用的ServletResponse
。不,它是我为服务器响应创建的类。它包含私有字符串字段。。。还有get/set方法。@bigjel你打算把它变成一个bean吗?它是请求范围还是单例范围?这些是您在考虑并发问题时应该回答的问题。@bigjel是的,一个请求-一个请求范围的bean。如果使用@Autowired
字段,Spring将注入一个代理,它知道如何检索请求范围的bean。