所在类:org.springframework.web.servlet.FrameworkServlet

    1. protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
    2. Class<?> contextClass = getContextClass();
    3. if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
    4. throw new ApplicationContextException( "Fatal initialization error in servlet with name '" +
    5. getServletName() +
    6. "': custom WebApplicationContext class [" + contextClass.getName()
    7. + "] is not of type ConfigurableWebApplicationContext");
    8. }
    9. // 通过反射创建 IOC 容器对象
    10. ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
    11. wac.setEnvironment(getEnvironment());
    12. //
    13. 设置父容器 wac.setParent(parent);
    14. String configLocation = getContextConfigLocation();
    15. if (configLocation != null) {
    16. wac.setConfigLocation(configLocation);
    17. }
    18. configureAndRefreshWebApplicationContext(wac);
    19. return wac;
    20. }