ͨ¹ýxmlÅäÖÃÎļþ
beanÅäÖÃÔÚxmlÀïÃæ£¬springÌṩ¶àÖÖ·½Ê½¶ÁÈ¡ÅäÖÃÎļþµÃµ½ApplicationContext.
µÚÒ»ÖÖ·½Ê½£ºFileSystemXmlApplicationContext
ͨ¹ý³ÌÐòÔÚ³õʼ»¯µÄʱºò£¬µ¼ÈëBeanÅäÖÃÎļþ£¬È»ºóµÃµ½BeanʵÀý:
ApplicationContext ac = new FileSystemXmlApplicationContext(¡°applicationContext.xml¡±)
ac.getBean(¡°beanName¡±);
µÚ¶þÖÖ·½Ê½£ºWebApplicationContextUtil
ÔÚB/SϵͳÖÐ,ͨ³£ÔÚweb.xml³õʼ»¯beanµÄÅäÖÃÎļþ£¬È»ºóÓÉWebAppliCationContextUtilµÃµ½ApplicationContext.ÀýÈ磺
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ÆäÖÐ servletContext sc ¿ÉÒÔ¾ßÌå »»³É servlet.getServletContext()»òÕß this.getServletContext() »òÕß request.getSession().getServletContext();
ÁíÍ⣬ÓÉÓÚspringÊÇ×¢ÈëµÄ¶ÔÏó·ÅÔÚServletContextÖеģ¬ËùÒÔ¿ÉÒÔÖ±½ÓÔÚServletContextÈ¡³öWebApplicationContext ¶ÔÏó£º
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);