-1

I want to do a dynamic injection of class in production. suppose we have one interface as Shape which provides an implementation for two classes namely Rectangle and Triangle.

for some classes, I want to provide Triangle instance and for some classes, I want to provide a Rectangle class instance.

So question is how to do this in Spring Boot

interface Shape{
}

class Rectangle implements Shape{
}


class Triangel implements Shape{
}
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
Pnk
  • 11
  • 2
  • Spring boot is same as Java only, is this your question? – iamrajshah Jan 24 '19 at 13:24
  • Possible duplicate of [Spring Boot - interfaces and implementations](https://stackoverflow.com/questions/30111451/spring-boot-interfaces-and-implementations) – Sanpas Jan 24 '19 at 13:33
  • you are not forced to use the spring container, you can create your instance yourself – rick Jan 24 '19 at 14:03
  • you can go with `@Qualifier` or `@Profile` or `@Conditional`.. [you can find @condition here](https://javapapers.com/spring/spring-conditional-annotation/) . – RAHUL ROY Jan 24 '19 at 14:26

1 Answers1

0

Welcome to SO

If you know at Compile time which implementation of Shape Class You want to inject then You can use @Qualifier Annotation. You can Find number of examples for this.

IF You want to inject Bean based on certain properties which can change at run time, Then You can use @Conditional Annotations. You will also find examples for this.

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104