首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring调整struts2时,ContextLoaderListener作用

2012-12-19 
spring整合struts2时,ContextLoaderListener作用1、添加ContextLoaderListener到web.xmllistenerlistene

spring整合struts2时,ContextLoaderListener作用
1、添加ContextLoaderListener到web.xml

<listener><listener-class>            org.springframework.web.context.ContextLoaderListener        </listener-class></listener>

如果需要添加多个spring配置文件就要在web.xml加入contextConfigLocation这个context参数
<context-param>         <param-name>contextConfigLocation</param-name>         <param-value>             /WEB-INF/classes/applicationContext-*.xml          </param-value>     </context-param> <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            /WEB-INF/classes/applicationContext-*.xml        </param-value>    </context-param>

2、ContextLoaderListener的作用
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。
3、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/classes/applicationContext-*.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;       }      .......}

热点排行