2

I am using the Spring/Springboot framework and would like to know if there is a possibility to "choose" or "select" the implementation class to instanciate depending on a configuration parameter

Let's say I have an interface IMyInterface and two possible implementations IImplentation1 and IImplementation2

I also have a class FooBar whose constructor receives a configuration class MyConf and an instance of IMyInterface

@Component
interface IMyInterface {
}

class  IImplentation1 implements IMyInterface {
}

@Component
class  IImplentation2 implements IMyInterface {
}



@Component
@ConfigurationProperties("my.application")
@Validated
class MyConf {
   String kind; /// could be an enum
}

class  IImplentation2 implements IMyInterface {
}


@Component
class FooBar {

    @Autowired
    public FooBar(MyConf myConf,
                  IMyInterface myInterface) { /// IImplentation1 or IImplentation2
        ...                                   /// depending on MyConf.kind
    }

}

Thanks for help

Regards Philippe

Philippe MESMEUR
  • 737
  • 8
  • 22

0 Answers0