springmvc3.2+spring+hibernate4全注解方式整合(一)

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>json_test</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加载所有的配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:com/config/spring/spring-*.xml</param-value>
  </context-param>
  
  <!-- 配置Spring监听 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置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>/WEB-INF/SrpingMvc_servlet.xml</param-value>
    </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
  
  
  <!-- 配置字符集 -->
  <filter>
      <filter-name>encodingFilter</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>
      <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Session -->
  <filter>
      <filter-name>openSession</filter-name>
      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>openSession</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

首先从我的web.xml文件大概能看出主要配置文件的存放路径和hibernate的版本。

 

2、springmvc的配置文件

<?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:p="http://www.springframework.org/schema/p"    
    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-3.0.xsd     
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context-3.0.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
     <!-- 注解探测器 -->
    <context:annotation-config />
    <!-- 添加注解驱动 -->  
    <mvc:annotation-driven />
    
     <!-- 启动扫描所有的controller 只扫描mvc,不扫描service -->
    <context:component-scan base-package="com.fangjian.core.web">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan> 
    
     <!-- 允许对静态资源文件的访问 -->   
    <mvc:default-servlet-handler /> 
    <mvc:resources mapping="/skin/**" location="/skin/" />  
    
    <!-- 定义跳转的文件的前后缀 -->  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    </bean>
    
    <!-- 上传文件大小限制为31M,31*1024*1024 -->
    <bean id="maxUploadSize" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="maxUploadSize" value="32505856" />  
        <property name="maxInMemorySize" value="4096" />  
    </bean> 
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 annotation默认的办法映射适配器 -->     
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />     
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" />
    
    
    <!-- 避免IE在ajax请求时,返回json出现下载 -->
   <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
</beans>

3、spring-jdbc.xml的配置文件

<?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:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.0.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <!-- 引入属性文件 -->  
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- Hibernate配置 -->    
    <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" id="dataSource">         
         <property name="driverClass" value="${jdbc.driver}">  </property>        
         <property name="jdbcUrl" value="${jdbc.url}">  </property>        
         <property name="user" value="${jdbc.username}">    </property>      
         <property name="password" value="${jdbc.password}">    </property>      
         <property name="autoCommitOnClose" value="true"> </property>         
         <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"> </property>         
         <property name="initialPoolSize" value="${cpool.minPoolSize}"> </property>         
         <property name="minPoolSize" value="${cpool.minPoolSize}"> </property>         
         <property name="maxPoolSize" value="${cpool.maxPoolSize}"> </property>         
         <property name="maxIdleTime" value="${cpool.maxIdleTime}"> </property>         
         <property name="acquireIncrement" value="${cpool.acquireIncrement}"> </property>         
         <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"> </property>   
    </bean> 
    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">         
        <property name="dataSource" ref="dataSource"></property>         
        
        <property name="packagesToScan" value="com.fangjian.core.platform.po"> </property>         
        <property name="hibernateProperties">             
            <props>                 
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>                 
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
            <!-- 
            <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProviderr</prop>                 
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>                                 
            <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>  
             -->               
            <prop key="current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>                  
            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>         
            <!-- <prop key="hibernate.cache.use_second_level_cache">true</prop> -->        
            <prop key="hibernate.cache.use_query_cache">false</prop>          
            <prop key="jdbc.use_scrollable_resultset">false</prop>        
            <!-- <prop key="hibernate.transaction.auto_close_session">false</prop> -->            
            </props>        
         </property>
     </bean> 
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >  
          <property name="dataSource" ref="dataSource"></property>
   </bean> 
   
    
    
    <!-- 事务管理器 -->
     
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
    
    
    
    <!-- 采用声明式容器管理事务一般只对service层进行处理 -->   
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->   
        <aop:pointcut id="txPointcut" expression="execution(* com.fangjian.core.platform.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>  
        <!-- 
        proxy-target-class="true"
        <aop:advisor pointcut="execution(* com.fangjian.core.platform.service.*.*(..))" advice-ref="txAdvice"/>
        <aop:advisor pointcut="execution(* com.fangjian.core.platform.service.*.*(..))" advice-ref="txAdvice"/>
         -->
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="merge*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
            <tx:method name="put*" propagation="REQUIRED" />  
            <tx:method name="use*" propagation="REQUIRED"/>  
            <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" rollback-for="java.lang.Exception"/>  
        </tx:attributes>
    </tx:advice>
   

</beans>

4、spring自己的配置文件spring-common.xml

<?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: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.xsd  
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
                        http://www.springframework.org/schema/tx  
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
    
    <!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在springMvc-servlet.xml中配置的,如果不去除会影响事务管理的。   --> 
    <context:component-scan base-package="com.fangjian.core">
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
    </context:component-scan>
    
</beans>

 

 

springmvc3.2+spring+hibernate4全注解方式整合(一),布布扣,bubuko.com

springmvc3.2+spring+hibernate4全注解方式整合(一)

上一篇:C++ double类型转string类型后,怎么实现小数点后只显示一个数字


下一篇:javascript中遇到Cannot read property 'split' of undefined问题