0

I have an interface and a class:

public interface FileProcessingService {
   String NAME = "fileProcessingService";

   void processFile(FileDescriptor fileDescriptor);
}

public class FileDescriptor(){
   protected File file;
   protected String type;
} 

And what I want to do is have it process different filetypes based on the content in the filedescriptor.
Is is possible to have a different service bean for each filetype. e.g. say i have the following filetypes:

Customer.txt
Supplier.txt

Have a different service bean for each?

Like follows:

@Service(FileProcessingService.NAME)
public class FileProcessingServiceBean implements FileProcessingService {

   @Override
   public boolean processFile(FileDescriptorExt fileDescriptor) {
      return false;
   }
}

public class CustomerFileServiceBean extends FileProcessingServiceBean{
   @Override
   public boolean processFile(FileDescriptorExt fileDescriptor) {
      System.out.println("IN CUSTOMER PROCESSOR");
      return false;
   }
}

public class SupplierFileServiceBean extends FileProcessingServiceBean{
   @Override
   public boolean processFile(FileDescriptorExt fileDescriptor) {
      System.out.println("IN SUPPLIER PROCESSOR");
      return false;
   }
}

So I can just call

Filedescriptor fd = new FileDescriptor();
fd.setType("Customer");
fd.setFile(Customer.txt);
fileProcessingService.processFile(fd);

And have it automatically process the file with the CustomerFileServiceBean. And if the file is not a customer file, have it process throught the super FileProcessingServiceBean...

Or is this not the right way to go about this? Like how do I get it to choose the appropriate bean e.g. with an annotation or such...? I guess it is downclassing a service - is that possible?

Daryn
  • 1,551
  • 1
  • 15
  • 21
  • Where you have used that service bean? – Gaurav Srivastav Jun 15 '18 at 07:10
  • Possible duplicate of [Strategy within Spring boot](https://stackoverflow.com/questions/44761709/strategy-within-spring-boot) – JEY Jun 15 '18 at 08:01
  • @JEY Thanks JEY, your suggestion put me in the right direction. Do you know if it is possible to set a default implementation if more than 1 beans match the criteria? e.g. I want to be able to set it to null and have it pick a default one. – Daryn Jun 15 '18 at 13:44
  • There is a good easy to follow example here: https://stackoverflow.com/questions/33993063/annotation-based-servicelocatorfactorybean – Daryn Jun 15 '18 at 13:44
  • Use annotation Primary – JEY Jun 15 '18 at 13:45
  • @JEY thanks man! ... And one more scenario. Is it possible to program it to use a default one if the bean name supplied does not exist? So instead of throwing an error I could get it to process it with the default one then maybe add some extra code to notify me that that bean does not exist? – Daryn Jun 15 '18 at 14:03
  • don't know the best things to do is use the primary and check each time the type – JEY Jun 15 '18 at 14:08

0 Answers0