1

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 class SpringDemoUploadingFilesApplication.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. 
}

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
Abby Howe
  • 86
  • 1
  • 12

4 Answers4

1

May be you can try adding @ComponentScan("com.example.springdemouploadingfiles") as given below, this is to explicitly mention to spring boot that you should scan that package

@ComponentScan("com.example.springdemouploadingfiles")
@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class SpringDemoUploadingFilesApplication {

    //all above processes here
}
Nithin
  • 683
  • 3
  • 11
1

I've been banging my head against a brick wall with this myself, and I think this particular tutorial is just a bit... meh.

It claims that you can follow the steps from scratch and arrive at the same point as if you download the source code from GitHub.

Like most Spring Getting Started guides, you can start from scratch and complete each step or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.

However, later on it explicitly states (at the "Create a File Upload Controller" part)...

The initial application already contains a few classes to deal with storing and loading the uploaded files on disk. They are all located in the com.example.uploadingfiles.storage package. You will use those in your new FileUploadController

So, here it's deviating from the initial premise, because there are classes in the ./initial directory of the source code you download from GitHub, but they aren't mentioned in the tutorial.

Once you copy those files from the com.example.uploadingfiles.storage classpath into your project, it should compile properly.

hubare
  • 48
  • 6
0

You probably are missing the implementation of StorageService:

You will need to provide a StorageService so that the controller can interact with a storage layer (such as a file system). The following listing (from src/main/java/com/example/uploadingfiles/storage/StorageService.java) shows that interface:

You can start with:

@Service
public class StorageServiceImpl implements StorageService {
    // Implement all the methods from StorageService.
}
Christophe L
  • 13,725
  • 6
  • 33
  • 33
  • I have it and just tried the @Service annotation to no avail. – Abby Howe Apr 13 '20 at 18:20
  • @AbigailHowe: your updated question shows that you added the `@Service` annotation on the interface. You need to create a concrete class that implements this interface and add the annotation there. – Christophe L Apr 14 '20 at 02:42
0

I had a similar error, everything seemed to be correct. And everything was correct. I solved it by running a maven clean and maven Build in eclipse IDE. If you get an error while running maven clean and maven Build, I invite you to see this (if you work with eclipse IDE): Spring Maven clean error - The requested profile "pom.xml" could not be activated because it does not exist