1.传统spring mvc配置
<!-- 省略非关键的配置 --> <!-- [1] Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <!-- ====================================== --> <!-- [2] Spring MVC配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> // 默认 </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
现在spring boot已经不需要配置了。通过自动配置代替了手动配置。但是这些配置还是存在
ContextLoaderListener
作用是在启动tomcat 或者jetty服务器的时候初始化一个Root Spring WebApplicationContext
public class ContextLoaderListener extends ContextLoader implements ServletContextListener { public ContextLoaderListener() { } public ContextLoaderListener(WebApplicationContext context) { super(context); } //负责初始化容器 public void contextInitialized(ServletContextEvent event) { this.initWebApplicationContext(event.getServletContext()); } //负责销毁容器 public void contextDestroyed(ServletContextEvent event) { this.closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); } }
initWebApplicationContext
<4>
text.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="remoteOssClient" class="com.bozhi.wemall.SimepleBeanFactory"> </bean> </beans>
web.xml
<context-param> <param-name>locatorFactorySelector</param-name> <param-value>classpath:test.xml</param-value> </context-param> <context-param> <param-name>parentContextKey</param-name> <param-value>remoteOssClient</param-value> </context-param>