当tomcat容器启动后,首先会寻找web.xml文件,加载其中的信息,并创建一个ServletContext上下文对象,在web.xml获取值,供整个web应用使用。
要使项目启动时就加载spring容器,就要在web.xml中配置spring容器。最先加载的就是节点,该节点加载我们的Spring配置文件,配置文件的内容是Spring容器的Bean。
有两种加载方式,
1.如果在web.xml中不指定,会默认去加载/WEB-INF/下的ApplicationContext.xml;
2.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/application-context.xml
/WEB-INF/config/zxw/zxw-context.xml
</param-value>
</context-param>
如果配置文件不再WEB-INF中,也可以在 param-value中添加classpath:xxx.xml来加载配置文件,如果是多个配置文件,中间用逗号相隔;classpath的含义是工程目录下也就是src路径下,下面还会阐述classpath与classpath*的区别。
光加载spring配置文件还不够,还需要在spring配置文件加载完实例化,做法是配置监听器listener,
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
监听器的作用是监听ServletContext的对象是否创建,web容器一旦启动,就会创