如何在spring boot中使用应用程序上下文获取bean

Qas*_*sim 37 java spring dependency-injection spring-boot

我正在开发一个SpringBoot项目,我想用它的名字来获取bean applicationContext.我已尝试过很多来自网络的解决方案,但无法成功.我的要求是我有一个控制器

ControllerA
Run Code Online (Sandbox Code Playgroud)

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

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

reo*_*eos 65

您可以将ApplicationContext自动装配为字段

@Autowired
private ApplicationContext context;
Run Code Online (Sandbox Code Playgroud)

或方法

@Autowired
public void context(ApplicationContext context) { this.context = context; }
Run Code Online (Sandbox Code Playgroud)

最后用

context.getBean(SomeClass.class)
Run Code Online (Sandbox Code Playgroud)

  • 现场注入被认为是不好的做法. (14认同)
  • 只是google"Field vs Constructor injection".此外,Oliver Gierke(Spring Data Team领导)反对现场注入. (4认同)
  • @luboskrnac:是的,很好奇为什么。 (2认同)
  • 那么应该如何更改这个答案才能使用构造函数注入呢? (2认同)
  • @crabbly Baeldung 不是 Spring 文档! (2认同)

Sun*_*amy 33

您可以使用ApplicationContextAware.

ApplicationContextAware:

接口由希望被通知ApplicationContext中,它运行英寸实现此接口有意义例如当对象需要访问一组协作bean的任何对象来实现.

有几种方法可以获得对应用程序上下文的引用.您可以实现ApplicationContextAware,如以下示例所示:

package hello;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    } 

 public ApplicationContext getContext() {
        return applicationContext;
    }

}
Run Code Online (Sandbox Code Playgroud)

更新:

春天实例豆,它查找了ApplicationContextAware实现,如果发现他们,()方法将被调用的setApplicationContext.

通过这种方式,Spring正在设置当前的 applicationcontext.

来自Spring的代码片段source code:

private void invokeAwareInterfaces(Object bean) {
        .....
        .....
 if (bean instanceof ApplicationContextAware) {                
  ((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
   }
}
Run Code Online (Sandbox Code Playgroud)

一旦获得对Application上下文的引用,就可以使用getBean()获取所需的bean.


Deb*_*ida 8

实际上,您想从Spring引擎中获取对象,该引擎已经在Spring应用程序启动时(Spring引擎的初始化)维护了所需类的对象。现在,您只需要将该对象获取到参考。

在服务班级

@Autowired
private ApplicationContext context;

SomeClass sc = (SomeClass)context.getBean(SomeClass.class);
Run Code Online (Sandbox Code Playgroud)

现在,在sc的参考中,您具有该对象。希望解释得很好。如有任何疑问,请通知我。


IKo*_*IKo 8

使用SpringApplication.run(Class<?> primarySource, String... arg)对我有用。例如:

@SpringBootApplication
public class YourApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(YourApplication.class, args);

    }
}
Run Code Online (Sandbox Code Playgroud)


Pap*_*AIL 5

作为替代方法,您可以使用获取任何用,或ConfigurableApplicationContext注释的类的 bean 。@Component@Repository@Service

假设您想要获取BaseComponent类的 bean :

@Service
public class BaseComponent {
    public String getMessage() {
        return "hello world";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你可以使用ConfigurableApplicationContext来获取 bean:

@Component
public class DemoComponent {
    @Autowired
    ConfigurableApplicationContext applicationContext;
    
    public BaseComponent getBeanOfBaseComponent() {
        return applicationContext.getBean(BaseComponent.class);
    }
}
Run Code Online (Sandbox Code Playgroud)