SpringMVC、hibernate 4およびspringの統合詳細構成

16328 ワード

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>