I am using Spring-Boot, initialize a class dynamically using string of class name and get the returned values

I am working in Spring Boot, and in MyService class I got a class name as String and i want to initialize that object and get back returned values.

but I have no much more idea about it, I think it achieved by Dependency injection. but, how ?

Suppose I have classes A.java, B.java, C.java and Service class MyService.java

@Component
public class A{
    public String wish(int timeHr){
       //some logic of time based wish
    return "Hello A"+" good morning";
}
}

@Component
public class B{
    public String wish(int timeHr){
       //some logic of time based wish
    return "Hello B"+" good morning";
}
}

@Component
public class C{
    public String wish(int timeHr){
       //some logic of time based wish
    return "Hello C"+" good morning";
}
}

@Service
public class MyService{
   // i get here A class name as String like,
   String classNameString = "A"; // or "B", or "C"
   int timrHr =  new Date().getHours();
   //how to here i will initialize above class and get method wist(param) returned wish msg.
   //like, a.wish(timeHr); and it gives "Hello A good morning"
}

I expect the output of returned by wish().

Can any one suggest me how do I achieved it ?

Thanks.

3
Leave a Reply

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

I can think of two ways here. First way is to name the class (after @Component) and using context, try to get the bean. You can also name the bean using @Qualifier @Component("A") public class A{ public String wish(int timeHr){ //some logic of time based wish return "Hello A"+" good morning"; } @Component("B") public class B{ public String wish(int timeHr){ //some logic of time based wish return "Hello B"+" good morning"; } } @Component("C") public class C{ public String wish(int timeHr){ //some logic of time based wish return "Hello C"+" good morning"; } } @Service public class MyService{ @Autowired private… Read more »

Jason
Guest

String is not a manageable bean by Spring IoC framework, but instances of classes A, B and C are your beans. What you should do is to declare the classes A, B and C as implement of the common interface and inject a corresponding bean by this type: interface Wisher { String wish(int timeHr); } @Component public class A implements Wisher { public String wish(int timeHr){ //some logic of time based wish return "Hello A"+" good morning"; } } @Component public class B implements Wisher { public String wish(int timeHr){ //some logic of time based wish return "Hello B"+" good… Read more »

Jason
Guest

Considering you are using Spring for implementation all your beans will be Singleton and these will be initialized at the start of App(when ApplicationContext is loaded) and then at that time application has no way to check which implementation to inject. So there is no way you can conditionally inject bean at runtime with Dependency injection instead you can use other design like below – @Service public class MyService{ private Wisher wisher; public Wisher setupWisher(String class){ if (class.equals("A")) { wisher = new A(); }else if(class.equals("B")){ wisher = new B(); }else if(class.equals("C")){ wisher = new C(); } } void myMethod(String requestString)… Read more »