`

看看Spring的contextLoader都做了什么

阅读更多

public class ContextLoader {

 

      

       public static final String CONTEXT_CLASS_PARAM = "contextClass";

 

//此处定义了spring配置文件的参数名称,web.xml中的

<context-param>

     <param-name>contextConfigLocation</param-name>

     <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dataAccessContext-IBatis.xml</param-value>

</context-param>

完全对应,这个名字是不可更改的

       public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

 

      

       public static final String LOCATOR_FACTORY_SELECTOR_PARAM = "locatorFactorySelector";

 

      

       public static final String LOCATOR_FACTORY_KEY_PARAM = "parentContextKey";

 

      

       private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";

 

 

       private static final Properties defaultStrategies;

 

       static {

              // Load default strategy implementations from properties file.

              // This is currently strictly internal and not meant to be customized

              // by application developers.

              try {

                     ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);

                     defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);

              }

              catch (IOException ex) {

                     throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());

              }

       }

 

 

       private final Log logger = LogFactory.getLog(ContextLoader.class);

 

       /**

       * The root WebApplicationContext instance that this loader manages.

       */

       private WebApplicationContext context;

 

       /**

       * Holds BeanFactoryReference when loading parent factory via

       * ContextSingletonBeanFactoryLocator.

       */

       private BeanFactoryReference parentContextRef;

 

 

      

       .......

      

       protected WebApplicationContext createWebApplicationContext(

                     ServletContext servletContext, ApplicationContext parent) throws BeansException {

 

              Class contextClass = determineContextClass(servletContext);

              if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {

                     throw new ApplicationContextException("Custom context class [" + contextClass.getName() +

                                   "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");

              }

 

              ConfigurableWebApplicationContext wac =

                            (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

              wac.setParent(parent);

              wac.setServletContext(servletContext);

//这里getInitParameter()方法概述:public java.lang.String getInitParameter(java.lang.String name)返回上下文定义的变量的值,如果变量不存在,返回null。见ServletConfig.getInitParameter (java.lang.String)

              String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);

//最后使用configLocation初始化wac对象

              if (configLocation != null) {

                     wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,

                                   ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));

              }

 

              wac.refresh();

              return wac;

       }

 

      

      .......

 

 

 

      

 

 

 

}

分享到:
评论

相关推荐

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoader.class org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web....

    spring源代码解析

    对于一个Spring激活的web应用程序,可以通过使用Spring代码声明式的指定在web应用程序启动时载入应用程序上下文(WebApplicationContext),Spring的ContextLoader是提供这样性能的类,我们可以使用 ...

    第17讲 Spring、hibernate和Struts2的整合

    ①在web.xml中配置ApplicationContext.xml,并使用ContextLoader监听器实例化spring容器 ②把action交给spring管理,即在spring配置文件中定义action Bean并使用依赖注入功能在action中注入业务Bean,同时修改作用域...

    SPRING API 2.0.CHM

    ContextLoader ContextLoaderListener ContextLoaderPlugIn ContextLoaderServlet ContextRefreshedEvent ContextSingletonBeanFactoryLocator ControlFlow ControlFlowFactory ControlFlowFactory.Jdk13...

    ContextLoader 加载xml

    NULL 博文链接:https://yjgyjg4.iteye.com/blog/662723

    struts2驱动包

    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:252) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190) ...

    S2SH整合报错

    ERROR main org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in...

    iriska:鸢尾花分类模型

    Iris model = new Iris ( new ContextLoader ()); Flower flower = Flower . builder() .setSepalLength( 5.57091678078844 ) .setPetalWidth( 3.09644861465126 ) .setPetalLength( 1.23725924762744 )

    realtor:Klassy的房地产经纪人回归模型

    =房地产经纪人= ... Houser model = new Houser ( new ContextLoader ()); House house = House . builder() .houseSize( 2983 ) .lotSize( 9365 ) .bedrooms( 5 ) .granite( 0 ) .bathroom( 1 ) .build

Global site tag (gtag.js) - Google Analytics