【摘要】 spring整合springmvc spring整合springmvc中web.xml配置如下,tomcat在启动过程中会加载web.xml中的内容,ContextLoaderListener实现了tomcat里面的ServletContextListener接口,所以在tomcat容器启动过程通过ContextLoaderListener来进行spring容器的初始化操作,并将 …
spring整合springmvc
spring整合springmvc中web.xml配置如下,tomcat在启动过程中会加载web.xml中的内容,ContextLoaderListener实现了tomcat里面的ServletContextListener接口,所以在tomcat容器启动过程通过ContextLoaderListener来进行spring容器的初始化操作,并将
classpath:spring/applicationContext-*.xml指定下的spring配置文件加载,该配置文件我只配置了<context:component-scan base-package=“org.com.yp”/>,代表通过扫描org.com.yp包下的类,包含@Component @Controller@Service等注解等类,进行bean注册。
bean注册是通过AbstractXmlApplicationContext.loadBeanDefinitions该类的方法进行bean定义加载的。
spring中加载bean定义是在org.springframework.context.ConfigurableApplicationContext#refresh方法中的ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()方法加载bean的,该方法之后会调用org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory方法创建bean工厂,并加载的bean定义。
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns=”http://java.sun.com/xml/ns/javaee”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd” id=”WebApp_ID” version=”3.0″>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!– 加载spring容器 –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!– 配置springMVC需要加载的配置文件–>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!– 默认匹配所有的请求 –>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
当tomcat容器启动后,通过路径访问资源时,第一次会调用org.springframework.web.servlet.HttpServletBean#init方法,之后的http请求就不会再方法该方法类;HttpServletBean实现了Servlet接口的规范,所以经过浏览器的请求经过servlet接口初始化执行init方法时,会再从spring容器中去加载springmvc配置中定义的加载类,spring与springmvc是父子容器的关系,下面是HttpServletBean的init方法
public final void init() throws ServletException {
// Set bean properties from init parameters.
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
if (!pvs.isEmpty()) {
try {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {
if (logger.isErrorEnabled()) {
logger.error(“Failed to set bean properties on servlet ‘” + getServletName() + “‘”, ex);
}
throw ex;
}
}
// 最后会调用org.springframework.context.ConfigurableApplicationContext#refresh容器的刷新方法,
// 进行springmvc容器初始化
initServletBean();
}
}