工具类:
public class SpringBeanFactoryUtils implements ApplicationContextAware {private static ApplicationContext appCtx;public static ApplicationContext getApplicationContext() {return appCtx;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {appCtx = applicationContext;}public static Object getBean(String beanName) {return appCtx.getBean(beanName);}
}
注入spring:
<bean id="springBeanFactoryUtils" class="test.utils.SpringBeanFactoryUtils"/>
获取bean:
TestService testService = (TestService ) SpringBeanFactoryUtils.getBean("testService Impl");