0

I had put Package name in component scan and @component also , but still error is coming . The code is below.

Here is the SpringConfig file:-

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

}

Here is the Service Class:-

@Component
public class Service {

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

    public String getText(String text) {
        //return anotherService.getText(text);
        return "hello";
    }
}

Here is the AnotherService class which is autowired in service class:-

@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";
        }
    }
}

Here is the IFileStoreService class autowired in AnotherService class:

public interface IFileStoreService {
Resource get(String id) throws StorageException, StorageFileNotFoundException;
}

Here is the IFileStoreImpl class:-

@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;
    }
}

And last MainHandler function:-

@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");

    }
}

and the Error is :-

{"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"}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
rockz
  • 121
  • 1
  • 11
  • Please try annotating the interface (IFileStoreService) with `@Component` instead of the implementation – maydawn Jul 09 '17 at 14:32
  • Done , but still same error is coming – rockz Jul 09 '17 at 14:37
  • Okay I apologize, I misread the error. I think the root of the problem is the last failed dependency injection I think. Last part of error says: `No qualifying bean of type [com.cagataygurturk.example.services.IFileStoreDAO]`. Are you sure that the `IFileStoreDAO` class (which you haven't posted here) is under the component scan, and annotated properly ? – maydawn Jul 09 '17 at 14:47
  • and does that interface have any implementing classes ? – maydawn Jul 09 '17 at 14:54
  • Thanks maydawn ," @component" in the implementation class of IFileStoreDAO. But why was it showing error in creating of bean of service class? – rockz Jul 09 '17 at 15:00
  • Glad I could help. Spring exceptions like that show nested exceptions. It starts with the highest level, and digs deeper. It could not create `service` bean because another bean inside it could not be instantiated because another bean.... and so on, until it reaches the bottom. – maydawn Jul 09 '17 at 15:14
  • Thanks maydawn.... – rockz Jul 09 '17 at 16:32
  • Hi @maydawn, as you solved the issue, please consider writing and answer to this question so others see that it already solved. Thanks! – ledniov Jul 09 '17 at 17:19
  • @ledniov okay, done – maydawn Jul 09 '17 at 18:18

1 Answers1

1

As the exception stacktrace suggests:

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.

That means that the class IFileStoreDAO does not have an implementation (I'm not 100% sure on this, might get different exception), or missing @Component annotation, or not being scanned by Spring as a component by being in packages not declared under @ComponentScan(basePackages={"com.cagataygurturk.example.lambda","com.cagataygurturk.example.services"}).

For more information regarding Spring Boot component scanning, see this answer: https://stackoverflow.com/a/33619632/5229041

maydawn
  • 498
  • 8
  • 20