Spring如何加载配置文件
面试的时候经常会有面试官问Spring知识点,面试官问到Spring是如何加载配置文件的,流程清楚吗?求职者:在web.xml中会指定pring配置文件路径,就会实现加载了。面试官:那你能说说流程吗?求职者:emmmm…这个时候就会很尴尬了。
任何事情不能局限于表面,需要有求知意识,尽自己能力和理解去探求真相,当然这个也不是一蹴而就,是一个循序渐进的过程,重要的是在这条路上需要不断求索。
spring加载配置文件实现流程
大家都知道,Spring配置Bean对象,默认都是在applicationContext.xml中,Spring启动的时候会加载配置文件,来看一段Spring容器配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这个配置大家都很熟悉,在web.xml中配置监听对象ContextLoaderListener,那这个对象中有什么,为什么要监听这个对象?我们先来看看这个对象中都有些什么
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
public ContextLoaderListener() {
}
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext