I am following this SpringBoot Demo on the Spring website to learn how to create a file that accepts uploads. I'm getting an error message:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.springdemouploadingfiles.FileUploadController required a bean of type 'com.example.springdemouploadingfiles.storage.StorageService' that could not be found.
Action:
Consider defining a bean of type 'com.example.springdemouploadingfiles.storage.StorageService' in your configuration.
Execution failed for task ':bootRun'.
Process 'command '/Applications/IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/bin/java'' finished with non-zero exit value 1
- I've tried to fix it by adding
@Bean
annotation to FileUploadController. - I've tried adding the following dependency
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2'
to build.gradle - NOTE: I don't want to connect a database to this demo project. The demo acknowledges that it's not trying to connect a database in this demo but that in a production environment we would.
- I've tried adding the
@Service
annotation above the Storage Service interface. - I've tried adding
@Component
above the StorageService interface. - I've tried adding the
@ComponentScan("com.example.springdemouploadingfiles")
annotation above the main application classSpringDemoUploadingFilesApplication.java
.
In any case, I'd love some help navigating this error so that I can run the demo. Below are my configurations.
build.gradle
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
FileUploadController.java
@Controller
public class FileUploadController {
private final StorageService storageService;
@Autowired
public FileUploadController(StorageService storageService) {
this.storageService = storageService;
}
SpringDemoUploadingFilesApplication.java
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class SpringDemoUploadingFilesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoUploadingFilesApplication.class, args);
}
@Bean
CommandLineRunner init(StorageService storageService) {
return (args) -> {
storageService.deleteAll();
storageService.init();
};
}
}
StorageService interface
@Service
public interface StorageService {
// implements interface.
}