简体   繁体   English

如何从不在Spring容器中的类访问Spring Bean的方法

[英]How to access a method a Spring Bean from a class not in the Spring Container

I'm not a Spring pro, so please bear with me.... 我不是春天职业选手,所以请耐心等待....

I have three classes: 我有三个班:

class SpringBeanA {
    public aMethod() {
        .....
    }
}

class SpringBeanB {

    @Autowired SpringBeanA a;

    public bMethod() {
        a.method();
    }
}

class NONSpringClass {
    .....
    b.method();
    .....
}

b.method() gives a null pointer error, both when accesed via the instances SpringBeanB b = new SpringBeanB() and autowiring SpringBeanB to NONSpringClass. b.method()在通过实例SpringBeanB b = new SpringBeanB()和自动装配SpringBeanB到NONSpringClass时都会产生空指针错误。

The autowiring: 自动装配:

class NONSpringClass {

    @Autowired SpringBeanB b;

    .....
    b.method();
    .....
}

How can I successfully call b.method() ? 如何成功调用b.method()

Spring initializes all the objects and keep it in Spring Application Context. Spring初始化所有对象并将其保存在Spring Application Context中。 You have couple different ways to get access to objects inside Application context 您可以通过几种不同的方式访问Application上下文中的对象

First create a spring configuration class to inject ApplicationContext in to a private attribute and expose as static method. 首先创建一个spring配置类,将ApplicationContext注入私有属性并作为静态方法公开。

@Configuration
class StaticApplicationContext implements ApplicationContextAware{

  static ApplicationContext applicationContext = null;

  public void setApplicationContext(ApplicationContext context)    throws BeansException {
    applicationContext = context;
  }
  /**
   * Note that this is a static method which expose ApplicationContext
   **/
  public static ApplicationContext getContext(){
        return applicationContext;
  }

}

Now you can try this in your non spring class, 现在你可以在非春季课上试试这个,

((SpringBeanB)StaticApplicationContext.getContext.getBean("b")).bMethod();

Please keep in mind, invoking getContext method before spring context initialization may cause NullPointerException. 请记住,在spring上下文初始化之前调用getContext方法可能会导致NullPointerException。 Also accessing beans outside of spring container is not a recommended approach. 也不建议在弹簧容器外部访问bean。 Ideal way will be to move all beans in to spring container to manage. 理想的方法是将所有豆子移入弹簧容器进行管理。

If you want to access SpringApplicationContext from a java Servelet please refer WebApplicationContextUtils 如果要从java Servelet访问SpringApplicationContext,请参阅WebApplicationContextUtils

A simple way to do this is using ApplicationContext.getBean(). 一种简单的方法是使用ApplicationContext.getBean()。

It's worth pointing out that this is considered bad practice, since it breaks inversion of control. 值得指出的是,这被认为是不好的做法,因为它打破了控制的倒置。

Either you make Spring manage your NONSpringClass and enable injection of SpringBeanB in NONSpringClass class or you have to manually inject a proper instance of SpringBeanB in your NONSpringClass reference. 要么你让春天管理NONSpringClass ,使注射SpringBeanBNONSpringClass类或你必须手动注入的适当实例SpringBeanBNONSpringClass参考。 In the latter approach, Spring has nothing to do, and you manually have to create the necessary instances and inject them using setters. 在后一种方法中,Spring无关,您需要手动创建必要的实例并使用setter注入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM