ContextLoadListener作用:
它會默認查找位于:WEB-INF/
下的是否有一個文件名稱為:applicationContext.xml
,spring必須要配置的元素有時會要把xml文件集中放在一個地方,這個時候就需要配置下:
web.xml
配置的話:
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
classpath*:**/*Context.xml
</param-value>
</context-param>
無web.xml
配置的:
public class DefaultConfigration implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext context) throws ServletException {
context.addListener(new ContextLoaderListener());
context.addListener(new WebAppRootListener());
context.setInitParameter("contextConfigLocation", "classpath*:**/*Context.xml");
}
}