com.ayman.image.Main中方法init的参数0需要一个类型为' com.ayman.image.storage.StorageService'的bean。无法找到

时间:2018-06-14 09:45:23

标签: java spring spring-boot javabeans

我有一个Spring应用程序。我在Windows上使用Eclipse IDE。

启动Spring应用程序时,我使用StorageService Interface实例化我的应用程序。

package com.ayman.image;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;


import com.ayman.image.storage.StorageService;

@SpringBootApplication
@ComponentScan("com.ayman.image.storage") //added this line by myself
public class Main{

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Bean
    CommandLineRunner init(StorageService storageService) {
        return (args) -> {
            storageService.deleteAll();
            storageService.init();
        };
    }
}

运行此Spring App时出现错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method init in com.mastercard.ayman.image.Main required a 
bean of type 'com.mastercard.ayman.image.storage.StorageService' that could 
not be found.


Action:

Consider defining a bean of type 
'com.ayman.image.storage.StorageService' in your configuration.

我使用组件扫描定义了我的Bean。这是错误的做法吗?

1 个答案:

答案 0 :(得分:0)

问题可能来自StorageService不是Spring bean。如果没有使用相关的spring注释定义bean,则应该像使用CommandLineRunner一样初始化它。

相关问题