我正在使用 Spring-Boot,使用类名字符串动态初始化一个类并获取返回值

我在 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() 返回的输出。


谁能建议我如何实现它?


跃然一笑
浏览 149回答 3
3回答

catspeake

我可以想到两种方法。第一种方法是命名类(在 @Component 之后)并使用上下文,尝试获取 bean。您还可以使用 @Qualifier 命名 bean@Component("A")public class A{&nbsp; &nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello A"+" good morning";}@Component("B")public class B{&nbsp; &nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello B"+" good morning";}}@Component("C")public class C{&nbsp; &nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello C"+" good morning";}}@Servicepublic class MyService{&nbsp; &nbsp;@Autowired&nbsp; &nbsp;private ApplicationContext context;&nbsp; void myMethod() {&nbsp; &nbsp; A a = (A) context.getBean("A");&nbsp; &nbsp; &nbsp;System.out.println(a.wish(123));&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; }第二种方法是让你所有的愿望类实现一个接口并遍历这个接口的所有实现 bean 并找到正确的 bean@Componentpublic class A implements Wisher{&nbsp; &nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello A"+" good morning";}@Componentpublic class B implements Wisher{&nbsp; &nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello B"+" good morning";}}@Componentpublic class C implements Wisher{&nbsp; &nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello C"+" good morning";}}public interface Wisher{ public String wish(int time); }@Servicepublic class MyService{&nbsp; &nbsp;@Autowired&nbsp; &nbsp;private List<Wisher> wishers;void myMethod() {&nbsp; &nbsp; String requiredClassName = "A";&nbsp; &nbsp; Wisher requiredWisher = null;&nbsp; &nbsp; for (int i = 0; i < wishers.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; Wisher w = wishers.get(i);&nbsp; &nbsp; &nbsp; &nbsp; if (w.getClass().getName().equals(requiredClassName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requiredWisher = w;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(w.wish(123));&nbsp; &nbsp; }}&nbsp; &nbsp; }

ABOUTYOU

考虑到您正在使用 Spring 实现,所有 bean 都将是 Singleton,并且这些 bean 将在 App 启动时(加载 ApplicationContext 时)进行初始化,然后应用程序无法检查要注入哪个实现。因此,您无法在运行时使用依赖注入有条件地注入 bean相反,您可以使用如下所示的其他设计 -@Servicepublic class MyService{&nbsp; private Wisher wisher;&nbsp; public Wisher setupWisher(String class){&nbsp; &nbsp;if (class.equals("A")) {&nbsp; &nbsp; &nbsp; &nbsp; wisher = new A();&nbsp; &nbsp; }else if(class.equals("B")){&nbsp; &nbsp; &nbsp; &nbsp; wisher = new B();&nbsp; &nbsp; }else if(class.equals("C")){&nbsp; &nbsp; &nbsp; &nbsp; wisher = new C();&nbsp; &nbsp; }&nbsp;}&nbsp;void myMethod(String requestString) {&nbsp; &nbsp; int timrHr =&nbsp; new Date().getHours();&nbsp; &nbsp; setupWisher(requestString).wish(timrHr);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}}

森栏

String 不是 Spring IoC 框架可管理的 bean,而是类的实例A,B并且C是您的 beans。你应该做的是声明类A,B并C作为公共接口的实现,并通过这种类型注入相应的 bean:interface Wisher {&nbsp; &nbsp;String wish(int timeHr);}@Componentpublic class A implements Wisher {&nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; &nbsp; return "Hello A"+" good morning";&nbsp; }}@Componentpublic class B implements Wisher {&nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; return "Hello B"+" good morning";&nbsp; }}@Componentpublic class C implements Wisher {&nbsp; public String wish(int timeHr){&nbsp; &nbsp; &nbsp;//some logic of time based wish&nbsp; return "Hello C"+" good morning";&nbsp; }&nbsp;}@Servicepublic class MyService{&nbsp; &nbsp;@Autowired&nbsp; &nbsp;private Wisher a; // instance of "A" or "B", or "C"&nbsp; &nbsp;void myMethod() {&nbsp; &nbsp; &nbsp; &nbsp; int timrHr =&nbsp; new Date().getHours();&nbsp; &nbsp; &nbsp; &nbsp; wisher.wish(timrHr);&nbsp; &nbsp;}&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java