更多得是通过IOC来把bean注入进去
这样就不会直接依赖spring
如果实在要通过getbean的方式来取得bean,这里有个通用的类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
public static Object getBean(String name){
return applicationContext.getBean(name);
}
}
将他配到spring配置文件里面去就ok了