zoukankan      html  css  js  c++  java
  • Spring整合SpringMVC

    实际上SpringMVC就运行在Spring环境之下,还有必要整合么?
    SpringMVC和Spring都有IOC容器,是不是都需要保留呢?

    答案是:通常情况下,类似于数据源,事务,整合其他框架都是放在spring的配置
    文件中(而不是放在SpringMVC的配置文件中),实际上放入Spring配置文件对
    应的IOC容器中的还有Service和Dao.而SpringMVC也搞自己的一个IOC容器,在
    SpringMVC的容器中只配置自己的Handler信息。

    1.导入Spring和SpringMVC的所有jar包
    c3p0-0.9.1.2.jar
    com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    commons-logging-1.1.3.jar
    mysql-connector-java-5.1.37-bin.jar
    spring-aop-4.0.0.RELEASE.jar
    spring-aspects-4.0.0.RELEASE.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
    spring-jdbc-4.0.0.RELEASE.jar
    spring-orm-4.0.0.RELEASE.jar
    spring-tx-4.0.0.RELEASE.jar
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar

    2.在web.xml文件中分别配置SpringMVC和Spring的配置信息
    <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>
    <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    3.配置spring的配置文件和springmvc的配置文件
    spring的配置文件:
    <!-- 配置扫描的包 -->
    <context:component-scan base-package="com.neuedu"></context:component-scan>
    <!-- 加载properties文件中 信息 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源 -->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.passowrd}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="driverClass" value="${jdbc.driver}"></property>
    </bean>

    <!-- 配置JdbcTemplate对应的bean, 并装配dataSource数据源属性-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="comboPooledDataSource"></property>
    </bean>
    <!-- 为了执行带有具名参数的SQL语句,需要配置NamedParameterJdbcTemplate -->
    <!-- 该NamedParameterJdbcTemplate类没有无参构造器,需要传入JdbcTemplate对象或者数据源对象[DataSource] -->
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <!-- 不能使用property标签配置哦 -->
    <constructor-arg ref="jdbcTemplate"></constructor-arg>
    </bean>

    springmvc的配置文件:
    <context:component-scan base-package="com.neuedu"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven></mvc:annotation-driven>

    加入jdbc.properties文件
    jdbc.user=root
    jdbc.passowrd=123456
    jdbc.url=jdbc:mysql://localhost:3306/jdbc_template
    jdbc.driver=com.mysql.jdbc.Driver

    4.创建Controller类与Service类,并创建这两个类的无参构造器,分别输出一句话!

    5.启动项目,会发现controller构造器和service构造器都执行了两次!
    问题:若Spring的IOC容器和SpringMVC的IOC容器扫描的包有重合的部分,就会导致有的
    bean会被创建2次!
    解决:
    1.使Spring的IOC容器扫描的包和SpringMVC的IOC容器扫描的包没有重合的部分!
    controller层都在controller包,service层都在service包
    2.但是有的时候开发的时候是分模块开发的,这样不太容易做到,所以:
    可以在component-scan标签下面中使用如下子标签来规定只能扫描的注解:
    <context:component-scan base-package="com.neuedu">
    <context:exclude-filter type="annotation" expression=""/>
    <context:include-filter type="annotation" expression=""/>
    </context:component-scan>

    所以在springMVC的配置文件中我们可以按着如下配置,只扫描controller及ControllerAdvice注解:
    <context:component-scan base-package="com.neuedu" 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>


    而在spring的配置文件中:
    <context:component-scan base-package="com.neuedu">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    此时再重新启动项目就会发现spring和springmvc的对象都创建了一份!

    6.Spring的IOC容器和SpringMVC的IOC容器的关系
    注意:
    1.SpringMVC容器中的bean可以引用Spring容器中的bean,
    也就是在Controller中我们可以注入service层对象【可以在controller层的requestMapping方法中打印service对象试一下】!
    2.反之则不行,就是说:在spring扫描的service层不能引用springmvc的handler
    对象【注解一个小例子,启动项目就会出错】
    3.实际上Spring的容器和Spring容器有父子间关系,【参考图片】就想儿子可以继承父亲的基因一样,父亲没法继承儿子的基因!
    而且从另一个角度也说明了Handler是可以依赖Service层的,但是Service层却不可以依赖Handler层!

  • 相关阅读:
    禁用生成8.3字符长文件名解决单目录存储大量文件写入性能下降问题
    遭遇EventSystem服务问题
    软件工程师遭遇硬件故障RAID5两块硬盘离线
    Pocket Player睡眠定时器不能选择问题的解决办法
    借助RamDisk让你的FireFox飞起来
    NVS4200M显卡“超频”
    Python数据类型
    Python变量
    端口及对应服务
    Python基础(1)
  • 原文地址:https://www.cnblogs.com/lc-java/p/7475389.html
Copyright © 2011-2022 走看看