目前认为比较好的解决方案。
1,工具类
public class SpringApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringApplicationContextHolder.context = context;
}
public static Object getSpringBean(String beanName) {
return context == null ? null : context.getBean(beanName);
}
public static String[] getBeanDefinitionNames() {
return context.getBeanDefinitionNames();
}
}
2,在Spring配置中注册工具类的bean
<bean class="xxx.SpringApplicationContextHolder" />
3,获取
Xxxx xx= (Xxxx) SpringApplicationContextHolder.getSpringBean("xxx");