java - Inject with Spring Boot depending on properties from application.yml -
in 1 of our common libraries our project need make distinction between 2 implementations of interface based on service using it.
i inject interface via constructor injection , need find out how can make sure implementation used based on value of property in our application.yml.
i looked @qualifier annotation seem need attribute in xml schema. don't have such thing.
in 1 part of our code read out properties our kafkalistener way
@kafkalistener(topics = "#{pathtoproperties.getprefix()}#ourbusinessproperties.getrelevantproperties()}"
can use same syntax in spring?
in 1 of our common libraries our project need make distinction between 2 implementations of interface based on service using it.
you can use profiles this.
let's have 2 profiles production , test.
then can annotate implementation want use in production this:
@profile("production") @component class productionimplementation implements myservice { }
and on component set different profile:
@profile("test") @component class testimplementation implements myservice { }
and start spring-boot application following argument:
-dspring.profiles.active=production
alternatively can select profile using environment variable:
spring_profiles_active = production
one more option create factory create different instance depending on environmental configuration:
@bean public myservice myservice() { if (condition) return firstimplementation(); return secondimplementation(); }
Comments
Post a Comment