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

标签 java spring spring-boot spring-mvc

我正在 Spring Boot 中工作,在 MyService 类 中,我得到了一个 String 类名称,我想初始化该对象并获取返回的值。

但我对此没有更多的了解,我认为它是通过依赖注入(inject)实现的。但是,怎么办?

假设我有类 A.java、B.java、C.java 和 Service 类 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() 返回的输出。

有人可以建议我如何实现这一目标吗?

谢谢。

最佳答案

我可以想到两种方法。第一种方法是命名类(在 @Component 之后)并使用上下文,尝试获取 bean。您还可以使用@Qualifier命名bean

@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 ApplicationContext context;

  void myMethod() {
    A a = (A) context.getBean("A");


     System.out.println(a.wish(123));
   } 
    }

第二种方法是让所有的 Wisher 类实现一个接口(interface),并迭代该接口(interface)的所有实现 bean,并找到正确的 bean

@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 morning";
}
}

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

public interface Wisher{ public String wish(int time); }

@Service
public class MyService{
   @Autowired
   private List<Wisher> wishers;

void myMethod() {
    String requiredClassName = "A";
    Wisher requiredWisher = null;
    for (int i = 0; i < wishers.length(); i++) {
        Wisher w = wishers.get(i);
        if (w.getClass().getName().equals(requiredClassName)) {
            requiredWisher = w;
            break;
        }

        System.out.println(w.wish(123));
    }
}



    }

关于java - 我正在使用 Spring-Boot,使用类名字符串动态初始化类并获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324941/

相关文章:

SpringMVC - 使用 mvcUrl 时出错 - 缺少 WebApplicationContext

java - 如何在spring-boot-2中添加WebRequestTraceFilter?

java - Spring Boot 无法呈现(非常)简单的 index.jsp

java - LazyCollectionOption.EXTRA 与 Spring 代理?

java - 轴突框架 : How to use state-stored aggregates

java - 发出 GET 请求以生成数独

java - 从 JSON 数组中提取数据

Java FX 应用程序加载事件

java - GridGain 模板搜索

java - Java中子类中过滤掉父类的记录