我正在Spring网站上关注此SpringBoot Demo,以学习如何创建一个可以接受上传的文件。我收到一条错误消息:

***************************
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
  • 我尝试通过将@Bean批注添加到FileUploadController来修复它。
  • 我尝试添加以下依赖项implementation 'org.springframework.boot:spring-boot-starter-data-jpa'runtimeOnly 'com.h2database:h2'build.gradle
  • 注意:我不想将数据库连接到此演示项目。该演示承认,它不是在此演示中尝试连接数据库,而是在生产环境中进行连接。
  • 我尝试在Storage Service界面上方添加@Service批注。
  • 我尝试在StorageService接口(interface)上方添加@Component
  • 我尝试在主应用程序类@ComponentScan("com.example.springdemouploadingfiles")上方添加SpringDemoUploadingFilesApplication.java批注。

  • 无论如何,我很乐意为您解决此错误提供帮助,以便我可以运行演示。以下是我的配置。

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

    最佳答案

    您可能缺少StorageService的实现:



    您可以从以下内容开始:

    @Service
    public class StorageServiceImpl implements StorageService {
        // Implement all the methods from StorageService.
    }
    

    07-27 15:13