Spring 和 Spring MVC 整合流程

Spring 和 Spring MVC 整合流程

虽然我们知道,Spring 和 Spring MVC 都是一套家族体系。但是对于不同的工人,就要干不同的活,而 Spring 和 Spring MVC 同样也要分开,干不同工人的活。

Spring 职责

  • 负责业务层和数据访问层

Spring MVC 职责

  • 负责控制层

所以也就是会存在两个 IOC 容器。

步骤

  1. 导入 jar 包或依赖

    jackson-annotations-2.9.0.jar
    jackson-core-2.9.0.jar
    jackson-databind-2.9.0.jar
    jboss-logging-3.1.1.GA.jar
    jstl.jar
    mysql-connector-java-8.0.19.jar
    spring-aop-5.0.2.RELEASE.jar
    spring-beans-5.0.2.RELEASE.jar
    spring-context-5.0.2.RELEASE.jar
    spring-core-5.0.2.RELEASE.jar
    spring-expression-5.0.2.RELEASE.jar
    spring-jcl-5.0.2.RELEASE.jar
    spring-jdbc-5.0.2.RELEASE.jar
    spring-orm-5.0.2.RELEASE.jar
    spring-tx-5.0.2.RELEASE.jar
    spring-web-5.0.2.RELEASE.jar
    spring-webmvc-5.0.2.RELEASE.jar
    

    这里尽可能的导入要用的 jar 包。

  2. 配置 Spring 的配置文件 applicationContext.xml,放在 classpath 路径下

    <?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:util="http://www.springframework.org/schema/util"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
      
      <context:component-scan base-package="com.nhky">
          <!-- 所有包都扫,就是不扫描带有 Controller 注解的类 -->
      	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          <!-- 所有包都扫,就是不扫描带有ControllerAdvice 注解的类 -->
    	<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 
      </context:component-scan>
    </beans>
    
  3. 配置 spring mvc 配置文件 springmvc-servlet.xml 放在 web-info 下

    <?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:util="http://www.springframework.org/schema/util"
           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-2.0.xsd
                               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- 只扫描带有 Controller 注解和 ControllerAdvice 注解的类 -->
    	<context:component-scan base-package="com.nhky" use-default-filters="false">
    		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    	</context:component-scan>
        <!-- 视图解析器,可以根据自己需要删改  -->
    	<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/jsp/"></property>
    		<property name="suffix" value=".jsp"></property>
    	</bean>
    	<!-- 开放静态资源访问权限 -->
    	<mvc:default-servlet-handler/>
    	<!-- 开启注解驱动 -->
    	<mvc:annotation-driven></mvc:annotation-driven>
    </beans>
    
  4. 配置 web.xml

    <?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" version="3.0">
      <display-name>testMVC</display-name>
    
      
      <!-- 配置 Spring 的 IOC 容器 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <!-- 配置 Spring MVC 的 IOC 容器 -->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    

    注意:

    • 这里的 spring mvc 的 servlet-name 要和 前面的配置文件 springmvc-servlet.xml 的前缀一样,也就是 “springmvc”
    • 其中在 servlet-name 中如果没有指定 init-param 属性,那么系统自动寻找的 spring 配置文件为 [servlet-name]-servlet.xml,并且默认会找 web-info 下面的文件。这也就为什么要求上面的 servlet-name 要和 配置文件的前缀一样。
    • Spring 的包扫描 和 Spring MVC 的包扫描要区分开来,各司其职。
    • 其中 Spring 不能拿到 Spring MVC IOC 容器中的 bean,而 Spring MVC 可以拿到 Spring IOC 容器中的 bean。它们是包含关系(Spring IOC 容器包含 Spring MVC IOC 容器)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值