9

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.

The autowiring:

class NONSpringClass {

    @Autowired SpringBeanB b;

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

How can I successfully call b.method()?

Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

3 Answers3

13

Spring initializes all the objects and keep it in Spring Application Context. You have couple different ways to get access to objects inside Application context

First create a spring configuration class to inject ApplicationContext in to a private attribute and expose as static method.

@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. Also accessing beans outside of spring container is not a recommended approach. 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

kamoor
  • 2,909
  • 2
  • 19
  • 34
1

A simple way to do this is using ApplicationContext.getBean().

It's worth pointing out that this is considered bad practice, since it breaks inversion of control.

Davis Yoshida
  • 1,757
  • 1
  • 10
  • 24
  • 1
    You cannot inject `ApplicationContext` in a non-spring bean. – Luiggi Mendoza Jan 18 '15 at 00:36
  • ApplicationContext is an interface, not a class – kamoor Jan 18 '15 at 00:38
  • I realize it's an interface. I should have been more clear by stating that some class implementing the ApplicationContext interface should be used. @Luiggi Mendoza, see kamoor's answer above for how to access an ApplicationContext from a non-spring bean. – Davis Yoshida Jan 19 '15 at 21:00
-1

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. In the latter approach, Spring has nothing to do, and you manually have to create the necessary instances and inject them using setters.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332