Eclipse上的“此位置不允许注释@Autowired”错误

Sou*_*bed 2 spring spring-mvc spring-boot

我正在尝试@Autowired用于文件存储服务,但显然我做错了。

我目前正在尝试在 Eclipse 上使用 Spring Boot 创建文件上传服务。

@PostMapping("/upload")
public String onUpload(@RequestParam("file")  MultipartFile file, CVtestForm cvForm, RedirectAttributes redirectAttributes) throws IOException {

    @Autowired
    StorageService storageService;


    return "upload_show";   
}
Run Code Online (Sandbox Code Playgroud)

我期待storageService连接到FileStorageService,它实现了它。

use*_*900 6

@Autowired不能用于局部变量,您应该将其添加到方法范围之外,例如作为字段:

@Autowired
StorageService storageService;

@PostMapping("/upload")
public String onUpload(@RequestParam("file")  MultipartFile file, CVtestForm cvForm, RedirectAttributes redirectAttributes) throws IOException {
Run Code Online (Sandbox Code Playgroud)

将构造函数、字段、setter 方法或配置方法标记为由 Spring 的依赖注入设施自动装配。