加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
class xxxx implements ApplicationContextAware{
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}
<bean id="xxxxxx" class="xxxxxxx"></bean>
方法,获得ApplicationContext对象
public static Object getBean(String beanId) {
return applicationContext.getBean(beanId);
}
在spring 配置文件里面要配置要用getBean获取的id
<bean id="commentService"
class="com.excellence.exoa.cms.service.impl.CommentServiceImpl">
</bean>