Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring 3 autowire in a standalone Java application

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?

like image 992
mike27 Avatar asked Sep 07 '10 14:09

mike27


People also ask

Can we use @autowire without using Spring boot context?

Sure you can use @Autowired but as your JobMain isn't a a Spring bean it will ofcourse not be autowired. Basically that is duplicate of this (more reasons here. So to fix make your JobMain an @Component and don't construct a new instance yourself.

Can we use @autowired in pojo?

The @Autowired annotation in spring automatically injects the dependent beans into the associated references of a POJO class. This annotation will inject the dependent beans by matching the data-type (i.e. Works internally as Autowiring byType).

Can we use @autowired without @component?

So the answer is: No, @Autowired does not necessarily mean you must also use @Component . It may be registered with applicationContext. xml or @Configuration+@Bean .

Can we use Autowire in normal class?

Yes you can , autowiree just ensures that autowired component is loaded when that class is loaded , if autowired component cant be wired it raises compile time exception .


2 Answers

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.

like image 142
Abhinav Sarkar Avatar answered Oct 10 '22 04:10

Abhinav Sarkar


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.

like image 25
Jan Bodnar Avatar answered Oct 10 '22 04:10

Jan Bodnar