所以我正在尝试使用Spring和Mybatis编写一个独立的应用程序。我使用相同的DAO和Web应用程序的东西,一切正常。我希望能够尽可能多地重用该代码来自动化流程。我需要自动装配一个DAO并从main()方法中使用它。现在,我有点模仿这个问题:Autowiring a Spring 3.2 standalone application fails所以我有类似
的东西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();
}
输出为空。我的DAO文件是一个接口,实现的接口类似于
@Repository
@Component
public class DAOImpl implements DAO{
@Autowired
mapper m;
//some methods
}
如何让它正确地自动装配?
编辑:正如所建议的那样,我试过这样做:
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);
}
它仍打印出null