Dependency Injection - Two objects that implement the same interface

1

I have two objects from the AmazonS3UploadService and DatabaseUploadService classes, and both implement the UploadService interface. The Uploading class has both objects injected by the Spring container.

The problem is that the container can not inject dependencies correctly and the following error occurs:

 Can not set br.com.manager.domain.service.AmazonS3UploadService field br.com.manager.domain.service.ConfiguracaoService.amazonS3UploadService to com.sun.proxy.$Proxy67

I tried to use other annotations as @Resource, @Inject, @Qualifier and I currently use @Autowired

Note: I do not know if it's a good idea to put files in the database for various reasons, but since it's a study project, I've put it as an option.

    
asked by anonymous 13.08.2015 / 15:20

2 answers

1

If you are injecting the two implementations, there is something wrong.

Injection dependency has as main motivation you only have one place to create the instances that implement each interface.

If the consumer really needs the two implementations, use different interfaces or (more sensibly) eliminate the dependency injection for this consumer and these services: it does not make sense to centralize the dependency if you are going to use multiple implementations.

    
13.08.2015 / 16:39
0

As much as injecting the two implementations at the same point in the system is strange, the problem has nothing to do with it. You probably asked for the injection using attributes of the concrete type, while you should have used the interface as attribute types. That way, you just put @Qualifier on top of each of the attributes that the injection would be done correctly.

Alberto

    
13.09.2015 / 20:29