springMVC、hibernate4及spring的整合详细配置

1、web.xml中配置:

该配置文件主要作用是启动服务器时加载初始化spring及springMVC。


<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Form Handlingdisplay-name>

      
     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>  

      
    <context-param>  
        <param-name>contextConfigLocationparam-name>  
        <param-value>classpath:spring/spring-servlet.xmlparam-value>  
    context-param> 
    
    <servlet>
        <servlet-name>HelloWebservlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        servlet-class>
        <init-param>  
            <param-name>contextConfigLocationparam-name>  
            <param-value>classpath:springmvc/springmvc-servlet.xmlparam-value>  
        init-param>  
    servlet>

    <servlet-mapping>
        <servlet-name>HelloWebservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

web-app>

2、spring配置文件:spring-servlet.xml
该配置文件主要是让spring管理bean(控制反转与依赖注入)。以及让spring管理hibernate(本示例将hibernate的配置全部由spring管理,不需要hibernate自己的配置文件。spring主要管理hibernate的事务及sessionFactory)。


<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:p="http://www.springframework.org/schema/p"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/redis 
    http://www.springframework.org/schema/redis/spring-redis.xsd">  
    
    
     
   <context:component-scan base-package="cn.test"/>   
           
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/cc" />
        <property name="username" value="root">property>
        <property name="password" value="123456">property>
    bean> 
     
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="false">
        
        <property name="dataSource" ref="dataSource" />        
        
        <property name="mappingLocations" value="classpath:cn/test/hibernate/pojo/*.hbm.xml">property>
        
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectprop>
              <prop key="hibernate.show_sql">trueprop>
           props>           
        property>
    bean>  
        
    <bean id="txManager"           class="org.springframework.orm.hibernate4.HibernateTransactionManager">    
        <property name="sessionFactory" ref="sessionFactory" />    
    bean>    

        
    <tx:annotation-driven transaction-manager="txManager"/>  
    
                  
  beans>

3、springMVC配置文件:springmvc-servlet.xml

  该配置文件是springMVC所需要的配置文件,该配置使用注解方式配置。

<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">  
   
   <context:component-scan base-package="cn.ct.hibernate.controller">
      <context:include-filter type="annotation"        expression="org.springframework.stereotype.Controller" />
   context:component-scan>
   
   <mvc:annotation-driven/>  
    
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   bean>
   
   
beans>

你可能感兴趣的:(spring)