Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 春季自动布线出现错误_Spring - Fatal编程技术网

Spring 春季自动布线出现错误

Spring 春季自动布线出现错误,spring,Spring,我已经在组件扫描和@component中输入了包名,但仍然会出现错误。代码如下 以下是SpringConfig文件:- @Configuration @ComponentScan(basePackages={"com.cagataygurturk.example.lambda","com.cagataygurturk.example.services"}) public class SpringConfig { } 以下是服务类别:- @Component public class Serv

我已经在组件扫描和@component中输入了包名,但仍然会出现错误。代码如下

以下是SpringConfig文件:-

@Configuration
@ComponentScan(basePackages={"com.cagataygurturk.example.lambda","com.cagataygurturk.example.services"})
public class SpringConfig {

}
以下是服务类别:-

@Component
public class Service {

    /**
     * Autowiring another Spring Bean
     */
    @Autowired
    AnotherService anotherService;

    public String getText(String text) {
        //return anotherService.getText(text);
        return "hello";
    }
}
下面是在服务类中自动连接的另一个服务类:-

@Component
public class AnotherService {
    @Autowired
    IFileStoreService file;
    public String getText(String text) {
        String t;
        t=(String)text;
        if(t.equals("get"))
        {
            file.get("1");
            return "You are in Get Controller and database is not connected\n";
        }
        else
        if(t=="post")
        {
            return "You are in post Controller and databse is not connecte\n";
        }
        else
        if(t=="delete")
        {
            return "You are int delete Controller and mongo database in not connected\n";
        }
        else
        {
            return "hii\n"+text+"hey";
        }
    }
}
以下是在另一个服务类中自动连接的IFileStoreService类:

public interface IFileStoreService {
Resource get(String id) throws StorageException, StorageFileNotFoundException;
}
以下是IFileStoreImpl类:-

@Component
public class FileStoreServiceImpl implements IFileStoreService {
    private static final Logger logger = LoggerFactory.getLogger(FileStoreServiceImpl.class);

    @Autowired
    private IFileStoreDAO fileStoreDAO;

    @Autowired
    private StorageProperties storageProperties;


    @Override
    public Resource get(String id) throws StorageException, StorageFileNotFoundException {
        Resource resource = null;
        File file = null;
        try {
            FileDetails fileDetails = fileStoreDAO.get(id);
            if(fileDetails != null) {
                String tempDir = storageProperties.getLocation();
                file = new File(tempDir + File.separator + fileDetails.getName());
                file.mkdirs();
                if(file.exists()) {
                    // p1: delete any file if existent in the directory;
                    file.delete();
                }
                file.createNewFile();
                FileCopyUtils.copy(fileDetails.getFileBytes(), file);
                resource = new UrlResource(Paths.get(tempDir).resolve(fileDetails.getName()).toUri());
            } else {
                throw new  StorageFileNotFoundException("No document found with id: " + id);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            if (e instanceof StorageFileNotFoundException) {
                throw (StorageFileNotFoundException) e;
            } else {
                throw new StorageException("", e);
            }
        }
        return resource;
    }
}
最后一个MainHandler函数:-

@SuppressWarnings("unused")
public class MainHandler
        extends AbstractHandler<SpringConfig>
        implements RequestHandler<Map<String,Object>, String> {

    static final Logger log = Logger.getLogger(MainHandler.class);
    @Override
    public String handleRequest(Map<String, Object> input, Context context)throws RuntimeException {
        // TODO Auto-generated method stub
        Service businessService = getApplicationContext().getBean(Service.class);
        return businessService.getText("hii");

    }
}

正如stacktrace的例外情况所示:

找不到依赖项类型为[com.cagataygurturk.example.services.IFileStoreDAO]的符合条件的bean:应至少有1个bean符合此依赖项的autowire候选项的条件。

这意味着类
IFileStoreDAO
没有实现(我不是100%确定,可能会得到不同的异常),或者缺少
@Component
注释,或者由于在
@ComponentScan(basePackages)下未声明的包中,Spring没有将其作为组件进行扫描={“com.cagataygurturk.example.lambda”、“com.cagataygurturk.example.services”})


有关Spring引导组件扫描的更多信息,请参阅以下回答:

请尝试注释接口(IFileStoreService)使用
@Component
而不是实现完成,但仍然会出现相同的错误。很抱歉,我误读了错误。我认为问题的根源是我认为最后一次失败的依赖项注入。错误的最后一部分是:
没有[com.cagataygurturk.example.services.IFileStoreDAO]类型的合格bean
。你确定
IFileStoreDAO
类(你没有在这里发布)在组件扫描下,并且被正确地注释了吗?该接口有任何实现类吗?感谢Maydown,“@component”,在IFileStoreDAO的实现类中。但是为什么它在创建服务类bean时显示错误呢?
{"errorMessage":"Error creating bean with name 'service': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cagataygurturk.example.services.AnotherService com.cagataygurturk.example.services.Service.anotherService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cagataygurturk.example.services.IFileStoreService com.cagataygurturk.example.services.AnotherService.file; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileStoreServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cagataygurturk.example.services.IFileStoreDAO com.cagataygurturk.example.services.FileStoreServiceImpl.fileStoreDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cagataygurturk.example.services.IFileStoreDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}","errorType":"org.springframework.beans.factory.BeanCreationException"}