"the annotation @Autowired is disallowed for this location" error on Eclipse

Viewed 6780

I'm trying to use @Autowired for a file storage service, but apparently I'm doing it wrong.

I'm currently trying to create a file upload service using Spring Boot on Eclipse.

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

    @Autowired
    StorageService storageService;


    return "upload_show";   
}

I'm expecting storageService be wired to FileStorageService, which implements it.

1 Answers

@Autowired can't be used for local variable, you should add it outside method scope, as field for example:

@Autowired
StorageService storageService;

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

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

Related