框架整合
web项目中Spring和SpringMVC框架启动
在web项目中,都会出现SpringMVC和Spring框架的整合;Web项目中SpringMVC框架的启动是通过前端控制器DispatcherServlet来实现的;而Spring框架的启动则是通过ContextLoaderListener监听器来实现的;
Web项目中SpringMVC框架启动:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Web项目中Spring框架启动:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
二者整合:
SpringMVC配置文件
在spring-mvc.xml配置文件中配置SpringMVC框架的几大组件的注解,同时扫描controller相关组件;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描controller组件-->
<context:component-scan base-package="com.demo.controller"/>
<!--开启SpringMVC组件注解:处理器映射和处理器适配器-->
<mvc:annotation-driven/>
</beans>
Spring配置文件
在spring.xml配置文件中配置除controller组件之外的其他组件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Spring框架扫描组件-->
<context:component-scan base-package="com.demo.service"/>
</beans>
二者整合
在启动配置文件web.xml中整合二者的配置,即将第一步中二者的启动配置放在一块就可以了;
整合原理
Web项目中Spring框架启动是通过Listener实现的,而Listener会在项目启动时就执行,所以Spring框架在项目启动时就已尽启动了;
而SpringMVC框架则是通过Servlet启动的,我们知道Servlet是在第一次使用时初始化和启动的,所以web项目启动时SpringMVC框架还没有启动,而是第一访问时初始化的,这时候Spring框架已经启动完成了,所以就可以获取到Spring的IOC容器设置成自己的父容器,然后从里面获取Bean组件来完成功能的调用了;这就是两个框架整合的原理;
整合小技巧
tomcat插件
普通的web项目需要下载和安装tomca服务器,然后将项目打成war包放到tomcat服务中,启动tomcat访问服务,这样做对开发来说是非常繁琐的,所以推荐通过tomcat插件的方式提高我们的开发和部署效率;
引用插件
使用plugin引入tomcat插件,如果下载失败则在插件中加入dependencies依赖就可以了;
<build>
<finalName>web-demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
使用插件
点击run后者debug启动即可