我正在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 @Service
批注。 @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.
}