如何在spring boot中使用应用程序上下文获取bean [英] How to get bean using application context in spring boot

查看:104
本文介绍了如何在spring boot中使用应用程序上下文获取bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个spring boot项目,我希望使用 applicationContext 来获取bean的名称。我在网上尝试了很多解决方案,但无法成功。我的要求是我有一个控制器

i am developing a spring boot project and i want to get the bean by its name using applicationContext. I have tried many solution on web but could not succeed. My Requirement is that i have a controller

ControllerA

并且内部控制器ai有一个方法 getBean(String className),我想获取已注册bean的实例。我有hibernate实体,我想通过仅在 getBean 方法中传递类的名称来获取bean的实例。

and inside controller a i have a method getBean(String className) , i want to get instance of registered bean. I have hibernate entities and i want to get an instance of bean by passing the name of class only in getBean method.

请帮助,如果有人知道解决方案

please help if someone know the solution

推荐答案

您可以自动装配ApplicationContext,作为字段

You can Autowire the ApplicationContext, either as a field

@Autowired
private ApplicationContext context;

或方法

@Autowired
public void context(ApplicationContext context) { this.context = context; }

最后使用

context.getBean(SomeClass.class)

这篇关于如何在spring boot中使用应用程序上下文获取bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆