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全注解方式整合(一)

时间: 2024-08-26 18:14:14

springmvc3.2+spring+hibernate4全注解方式整合(一)的相关文章

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

以上是工程文件,下面开始测试 package test.testservice; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.t

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

service接口 package com.fangjian.core.platform.service; import com.fangjian.core.platform.po.User; public interface UserService { void saveUser(User user); } service实现 package com.fangjian.core.platform.service.impl; import org.springframework.beans.fa

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

jdbc.properties #hibernate settings hibernate.show_sql=true hibernate.format_sql=true hibernate.cache.use_query_cache=true hibernate.cache.provider_class=net.sf.ehcache.hibernate.SingletonEhCacheProvider #mysql version database setting jdbc.driver=co

6.Spring+Struts+Hibernat注解方式整合

SSH注解方式实现 1.创建db.properties文件,主要是数据库的连接数据 jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/testjdbc.username=rootjdbc.password=076634 2.创建Struts的配置文件,主要定义struts的常规配置 <?xml version="1.0" encoding="UTF-8&qu

java spring mvc 全注解

本人苦逼学生一枚,马上就要毕业,面临找工作,实在是不想离开学校.在老师的教导下学习了spring mvc ,配置文件实在繁琐,因此网上百度学习了spring mvc 全注解方式完成spring的装配工作; 废话不多说了上干货,其实我也没怎么理解不过简单的运行了一个spring mvc 全注解项目,也不能说是全注解,因为保留了web.xml和spring-serlvet.xml文件,(可能有的童鞋会说,这样配置可能对以后的修改不方便,无法达到只修改配置文件就切换某些环境.其实不是,零配置文件只是修

基于已构建S2SH项目配置全注解方式简化配置文件

如果还不熟悉s2sh项目搭建的朋友可以先阅读 eclipse环境下基于已构建struts2项目整合spring+hibernate 这两篇文章熟悉一下. 本文是基于以上两篇文章的基础构建的,以下给出全注解方式配置S2SH项目的参考步骤. 第一步:实体类映射数据库表,简化hibernate通过xml配置文件映射 首先我们新建实体类作为测试,包结构如图所示: 新建User到model包下,实体类字段信息如下所示: package wjt.com.test.model; import javax.pe

spring AOP自定义注解方式实现日志管理

转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在applicationContext-mvc.xml中要添加的 <mvc:annotation-driven />     <!-- 激活组件扫描功能,在包com.gcx及其子包下面自动扫描通过注解配置的组件 -->     <conte

一个简单的Spring定时器例子 注解方式

首先在applicationContext.xml中增加 文件头中增加一条 xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation 中增加一条 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd <beans xmlns:task=&quo

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Respository:标识持久层组件 3.@Service:标识业务层组件 4.@Controller:标识表现层组件 Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称 当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件