0

Is there a way to run Spring application configured by annotation, directly from main method? I'm still getting NullPointerException runing code below. Note that I'm not using SpringMVC and when I'm using beans defined inside context.xml, injected inside main method by context.getBean method, all code works perfect (without error) - but I just wondering if it is a way to manage runing this only with annotations?

//IElem.java
package com.pack.elem;
public interface IElem {
   public abstract String sayHello();
}



//Elem.java
package com.pack.elem;
import org.springframework.stereotype.Component;
@Component
public class Elem implements IElem{
   @Override
   public String sayHello() {
       return ("Hello from Elem class object");
   }
}


//RunElem.java
package com.pack.elem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RunElem {
   @Autowired
   private IElem elem;

   public void runHello(){
      System.out.println(elem.sayHello());
   }
}


//Main.java
package com.pack.main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pack.elem.RunElem;

public class Main {
   @Autowired
   private RunElem runelem;

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

   private void runRun(){
      runelem.runHello();
   }
 }


<!--/META-INF/application-context.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <context:annotation-config/>
   <context:component-scan base-package="com.pack"/>
</beans>
kropla
  • 163
  • 10
  • So you expect a instance created outside the scope of spring to be automagically managed by Spring. You shouldn't be constructing instances yourself but should use the one which is already configured by spring. In other words retrieve it from the `ApplicationContext` instead of constructing your own. – M. Deinum Dec 09 '13 at 12:06
  • @RakeshGoyal you have right. It is similar. I didn't found this before. Maybe it would help. Thanks. – kropla Dec 09 '13 at 12:54
  • @M.Deinum now it is clear for me. Thanks! – kropla Dec 09 '13 at 12:55

1 Answers1

2

Put simply, Spring is autowiring if it is the one instantiating the bean. So you have to use getBean(). If you want to use annotations only, something like this should work:

@Component
public class Main {
   @Autowired
   private RunElem runelem;

   public static void main(String[] args) {
       ApplicationContext context= new ClassPathXmlApplicationContext("/META-INF/application-context.xml");
    Main mainBean = context.getBean(Main.class);
    mainBean.runRun();
   }

   private void runRun(){
      runelem.runHello();
   }
 }

Alternatively you can autowire an existing bean as shown below, but this is not a recommended Spring usage:

public class Main {
   @Autowired
   private RunElem runelem;

   public static void main(String[] args) {
       ApplicationContext context= new ClassPathXmlApplicationContext("/META-INF/application-context.xml");
    Main mainBean = new Main();
    context.getAutowireCapableBeanFactory().autowireBean(mainBean);
    mainBean.runRun();
   }

   private void runRun(){
      runelem.runHello();
   }
 }
Adam Dyga
  • 8,666
  • 4
  • 27
  • 35