public class SpringContextHolder implements ApplicationContextAware {
private static final Logger logger = Logger.getLogger(SpringContextHolder.class);
private static ApplicationContext ctx=null;
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextHolder.ctx=context;
}
private SpringContextHolder(){
}
public static ApplicationContext getCtx(){
return ctx;
}
public static Object getBean(String name){
return ctx.getBean(name);
}
}
加載Spring配置文件時,若是Spring配置文件中所定義的Bean類實現了ApplicationContextAware 接口,那麼在加載Spring配置文件時,會自動調用ApplicationContextAware 接口中的html public void setApplicationContext(ApplicationContext context) throws BeansException java 方法,設置ApplicationContext對象。web 前提必須在Spring配置文件中指定該類spring |
Properties properties = (Properties) SpringContextHolder.getBean("settings");