在一个基于SSM(Spring + Spring MVC + MyBatis)框架的Web应用程序中,启动过程涉及到Spring的IOC容器和MVC容器的初始化。
IOC容器初始化:
applicationContext.xml文件中的配置来完成。
<context:component-scan base-package="com.your.package" />
<import resource="classpath:spring-mybatis.xml" />
上述配置中的用于扫描指定包下的类,将其注册为Spring的Bean。导入了MyBatis的配置文件。
MVC容器初始化:
dispatcher-servlet.xml中完成。
<context:component-scan base-package="com.your.web.package" />
<mvc:annotation-driven />
<import resource="classpath:spring-mybatis.xml" />
上述配置中的用于扫描Web层的类,将其注册为Spring MVC的Controller。启用基于注解的Spring MVC特性。
整合MyBatis:
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
bean>
在上述配置文件中,用于加载属性文件,配置数据源和其他MyBatis相关的Bean。
启动过程:
web.xml文件中的Servlet容器配置触发。
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:application-context.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:dispatcher-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
上述和配置用于启动Spring的IOC容器,而和配置用于启动Spring MVC的DispatcherServlet。