spring会帮我们自动注入bean,但如果我们要在普通类中获取bean该如何呢:
@Component
public class SpringUtil implements BeanFactoryAware {
private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
SpringUtil.beanFactory = beanFactory;
}
public static Object getBean(String beanName){
return beanFactory.getBean(beanName);
}
}