I am trying to use a file storage service in a REST controller. I am using the StorageService
from this Spring Boot tutorial. This is the controller class:
@RestController
public class MessageController {
@Autowired
private StorageService storageService;
}
And this is the service class:
@Service
public interface StorageService {
void init();
void store(MultipartFile file);
Path load(String filename);
Resource loadAsResource(String filename);
Stream<Path> loadAll();
void deleteAll();
}
This empty controller class with the service autowired results in a runtime error:
Field storageService in com.wais.app.MessageApp.controllers.MessageController required a
bean of type 'com.wais.app.MessageApp.services.StorageService' that could not be found.
I've also come across this thread: Spring Boot @autowired does not work, classes in different package, but in my case the main application class (which is annotated with @SpringBootApplication
) resides in package com.wais.app.MessageApp
, the service resides in com.wais.app.MessageApp.services
and the controller resides in com.wais.app.MessageApp.controllers
. Moreover, I can successfully autowire repositories from package com.wais.app.MessageApp.repositories
in the same controller.
What is going on here?