73

Here is my code:

public class Main {

    public static void main(String[] args) {
        Main p = new Main();
        p.start(args);
    }

    @Autowired
    private MyBean myBean;
    private void start(String[] args) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("META-INF/config.xml");
        System.out.println("my beans method: " + myBean.getStr());
    }
}

@Service 
public class MyBean {
    public String getStr() {
        return "string";
    }
}

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:annotation-config /> 
    <context:component-scan base-package="mypackage"/>
</beans>

Why doesn't this work? I get NullPointerException. Is it possible to use autowiring in a standalone application?

Jonik
  • 80,077
  • 70
  • 264
  • 372
mike27
  • 733
  • 1
  • 6
  • 4

5 Answers5

133

Spring works in standalone application. You are using the wrong way to create a spring bean. The correct way to do it like this:

@Component
public class Main {

    public static void main(String[] args) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("META-INF/config.xml");

        Main p = context.getBean(Main.class);
        p.start(args);
    }

    @Autowired
    private MyBean myBean;
    private void start(String[] args) {
        System.out.println("my beans method: " + myBean.getStr());
    }
}

@Service 
public class MyBean {
    public String getStr() {
        return "string";
    }
}

In the first case (the one in the question), you are creating the object by yourself, rather than getting it from the Spring context. So Spring does not get a chance to Autowire the dependencies (which causes the NullPointerException).

In the second case (the one in this answer), you get the bean from the Spring context and hence it is Spring managed and Spring takes care of autowiring.

Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
  • Isn't @Autowired an all-in strategy? Either spring manages _all_ the object creation none, but you can't add @Autowired to some fields in your callstack and instantiate them with new ..(). – Cojones Mar 18 '11 at 17:16
  • If only one single object in your callstack is instantiated with new ..() it won't work right? – Cojones Mar 18 '11 at 17:23
  • 3
    @Cojones, you can autowire some beans and create others with new, otherwise how would you ever be able to call `new ArrayList()`, for example? If you have a class with autowired parameters and you instantiate it with `new` then the autowiring won't take place. – Paul Jul 30 '11 at 19:24
  • 5
    You might need this in your config.xml also:` ` – Mikael Vandmo Apr 09 '13 at 13:30
  • 2
    Don't forget the @Component annotation above Main, otherwise you'll get a No qualifying bean of type [..] is defined exception. Took me some time to figure out. – TinusSky Jun 22 '14 at 11:56
  • 1
    @TinusSky even with that I got No qualifying bean of type..error – vaske Jun 22 '16 at 23:43
  • I get `org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [Main] is defined` =( – naXa stands with Ukraine Jun 21 '17 at 14:27
29

Spring is moving away from XML files and uses annotations heavily. The following example is a simple standalone Spring application which uses annotation instead of XML files.

package com.zetcode.bean;

import org.springframework.stereotype.Component;

@Component
public class Message {

   private String message = "Hello there!";

   public void setMessage(String message){

      this.message  = message;
   }

   public String getMessage(){

      return message;
   }
}

This is a simple bean. It is decorated with the @Component annotation for auto-detection by Spring container.

package com.zetcode.main;

import com.zetcode.bean.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.zetcode")
public class Application {

    public static void main(String[] args) {

        ApplicationContext context
                = new AnnotationConfigApplicationContext(Application.class);

        Application p = context.getBean(Application.class);
        p.start();
    }

    @Autowired
    private Message message;
    private void start() {
        System.out.println("Message: " + message.getMessage());
    }
}

This is the main Application class. The @ComponentScan annotation searches for components. The @Autowired annotation injects the bean into the message variable. The AnnotationConfigApplicationContext is used to create the Spring application context.

My Standalone Spring tutorial shows how to create a standalone Spring application with both XML and annotations.

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
  • This solution is much much easier than the xml solutions. Worked for me really well. – Kyle Bridenstine Apr 03 '18 at 23:52
  • I am curious why it is needed to autowire and then get the bean from the context.. I think my answer resolves this antipattern – Michail Michailidis Jun 22 '19 at 08:56
  • 1
    @MichailMichailidis You have a Spring Boot application. Here we deal with a classic Spring application. We need to bootstrap the application. – Jan Bodnar Jun 22 '19 at 20:44
  • Thank you @JanBodnar. I thought all apps now by default are SpringBootApplications either extending CommandLineRunner resembling what you are describing or otherwise starting a web server – Michail Michailidis Jun 22 '19 at 20:56
11

For Spring 4, using Spring Boot we can have the following example without using the anti-pattern of getting the Bean from the ApplicationContext directly:

package com.yourproject;

@SpringBootApplication
public class TestBed implements CommandLineRunner {

    private MyService myService;

    @Autowired
    public TestBed(MyService myService){
        this.myService = myService;
    }

    public static void main(String... args) {
        SpringApplication.run(TestBed.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        System.out.println("myService: " + MyService );
    }

}

@Service 
public class MyService{
    public String getSomething() {
        return "something";
    }
}

Make sure that all your injected services are under com.yourproject or its subpackages.

Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
3

I case you are running SpringBoot:

I just had the same problem, that I could not Autowire one of my services from the static main method.

See below an approach in case you are relying on SpringApplication.run:

@SpringBootApplication
public class PricingOnlineApplication {

    @Autowired
    OrchestratorService orchestratorService;

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

        application.start();
    }

    private void start() {
        orchestratorService.performPricingRequest(null);
    }

}

I noticed that SpringApplication.run returns a context which can be used similar to the above described approaches. From there, it is exactly the same as above ;-)

gerstams
  • 415
  • 2
  • 12
0

A nice solution would be to do following,

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

@Component
public class SpringContext implements ApplicationContextAware {

private static ApplicationContext context;

/**
 * Returns the Spring managed bean instance of the given class type if it exists.
 * Returns null otherwise.
 * @param beanClass
 * @return
 */
public static <T extends Object> T getBean(Class<T> beanClass) {
    return context.getBean(beanClass);
}

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {

    // store ApplicationContext reference to access required beans later on
    SpringContext.context = context;
}
}

Then you can use it like:

YourClass yourClass = SpringContext.getBean(YourClass.class);

I found this very nice solution in the following website: https://confluence.jaytaala.com/pages/viewpage.action?pageId=18579463

G. Frx
  • 2,350
  • 3
  • 19
  • 31