由于有时会写一些工具类或者单例类,这些类中可能使用到Spring管理的一些类,比如dao,service等,这时就要获得这些spring管理的类,我们写测试类中main函数中一般是用:
ApplicationContext ac =
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
BeanFactory bf = (BeanFactory)ac;
StaffService staffService = (StaffService)bf.getBean(staffService);
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
IEMailSendService emailSendService = (IEMailSendService) wac.getBean("emailSendService");
但这种方法只适合web工程。
参考:http://blog.itpub.net/143526/viewspace-1058439/