我在 Spring Boot 中工作,MyService class我得到了一个名为 String 的类,我想初始化该对象并取回返回值。
但我对此没有更多的想法,我认为它是通过依赖注入实现的。但是,怎么办?
假设我有类A.java, B.java, C.java和服务类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"
}
我期望 wish() 返回的输出。
谁能建议我如何实现它?
catspeake
ABOUTYOU
森栏
相关分类