0

So I'm trying to write a standalone application using Spring and Mybatis. I used the same DAO's and stuff for a web application, and everything works fine. I want to be able to reuse as much of that code as possible to automate a process. I need to autowire a DAO and use it from a main() method. Right now, I have it modeled somewhat off of this question: Autowiring a Spring 3.2 standalone application fails so I have something like

public class Main{

    @Autowired
    DAO dao; 

    public Main(){
        final ApplicationContext context = new FileSystemXmlApplicationContext("src/beans.xml");
        AutowireCapableBeanFactory acbFactory = context.getAutowireCapableBeanFactory();
        acbFactory.autowireBean(this);
        System.out.println(dao); 
    }

    public static void main(String[] args) throws Exception {
        Main m = new main(); 
    }

The output is null. My DAO file is an interface and the implemented interface has something like

@Repository
@Component
public class DAOImpl implements DAO{
    @Autowired
    mapper m;

    //some methods
}

How do I get it to autowire properly?

EDIT: As suggested, I tried doing this:

public class Main{

    @Autowired
    DAO dao; 

    public Main(){
    }

    public static void main(String[] args) throws Exception {
        Main m = new main(); 
        final ApplicationContext context = new FileSystemXmlApplicationContext("src/beans.xml");
        AutowireCapableBeanFactory acbFactory = context.getAutowireCapableBeanFactory();
        acbFactory.autowireBean(m);
        System.out.println(m.dao); 
    }

It still prints out null

Community
  • 1
  • 1
DFL
  • 173
  • 1
  • 14
  • What is `test`? Is it related to the code you are showing? – geoand Jun 17 '14 at 19:04
  • I strongly suggest that you whole code you are using in the constructor to the `main` method (after of course you have created an instance of the `Main` class inside it) – geoand Jun 17 '14 at 19:09
  • Whoops. I had Main named as "test" but changed it when I copied it over to here. I'll change that now – DFL Jun 17 '14 at 19:09
  • No problem! Take a look at my second comment. I think that if you follow it, your problem will go away. The reason I am am suggesting it, is because the object has not yet fully complete construction when you pass it the the `AutowireCapableBeanFactory` – geoand Jun 17 '14 at 19:10
  • I just tried that. I still get a null. – DFL Jun 17 '14 at 19:15
  • Can you update your question with the updated code you used? – geoand Jun 17 '14 at 19:17
  • Your code works for me (your EDITed version). My .xml contains just the `component-scan` element, my Dao is very simple with no autowire in it, it does implement an interface and is annotated with `@Component`. – Andrei Stefan Jun 18 '14 at 01:13
  • Are you sure you are using the correct `beans.xml`? – Andrei Stefan Jun 18 '14 at 01:16
  • Yeah, after some tweaking, it seems that the issue is in my beans.xml. After fixing it, it seems to hang after the ApplicationContext is declared. – DFL Jun 18 '14 at 13:17

0 Answers0