gpt4 book ai didi

java - 通过字符串调用引用

转载 作者:搜寻专家 更新时间:2023-11-01 03:54:35 24 4
gpt4 key购买 nike

在我的代码中,我有一个 HPContentModule:

@Resource(name = "HPContentModule")
private HPContentModule hpContentModule;

我想知道是否有可能获得这样的 String:

String myString = "hpContentModule";

这样我就可以使用 myString 调用 hpContentModule,执行类似 myString.init(); 的操作,其中 init( )HPContentMethod 中的方法吗?

或者,

如果我有 bean :

<beans:bean id="myBean" class="com.app.search.HPContent" />

我可以在 Controller 中通过 String 调用这个 bean 吗?

最佳答案

有多种方法可以做到这一点。

最简单的方法之一是使用 Spring 的 ApplicationContext。有several ways您可以获得对 ApplicationContext 的访问权限。获得 ApplicationContext 的副本后,您可以像这样调用 getBean() 方法:

HPContentModule hpContentModule = (HPContentModule) appContext.getBean( "myBean" );

关于java - 通过字符串调用引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12431149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com