首页 > 代码库 > spring整合hibernate的applicationContext.xml文件配置以及web.xml

spring整合hibernate的applicationContext.xml文件配置以及web.xml

applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 

       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:aop="http://www.springframework.org/schema/aop"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

  xsi:schemaLocation="http://www.springframework.org/schema/beans 

  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 

  http://www.springframework.org/schema/tx 

  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

  http://www.springframework.org/schema/aop 

  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

  <!-- 配置hibernate连接信息 -->

  <bean id="source" class="org.apache.commons.dbcp.BasicDataSource">

    <property name="driverClassName" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"/>

    <property name="url" value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:orcl"/>

    <property name="username" value="http://www.mamicode.com/james"/>

    <property name="password" value="http://www.mamicode.com/james"/>

  </bean>

  <!-- 配置sessionFactory -->

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource" ref="source"/>

    <!-- 配置hibernate其他参数 -->

    <property name="hibernateProperties">

      <props>

        <prop key="hibernate.show_sql">true</prop>

        <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

        <prop key="javax.persistence.validation.mode">none</prop>

      </props>

    </property>

    <!-- 配置hibernate映射文件,非注解情况下 -->

    <property name="mappingResources">

      <list>

        <value>entity/Comments.hbm.xml</value>

        <value>entity/News.hbm.xml</value>

        <value>entity/User.hbm.xml</value>

        <value>entity/Topic.hbm.xml</value>

      </list>

    </property>

  </bean>

  <!-- 配置dao注入sessionFactory -->

  <bean id="userDao" class="dao.UserDaoImpl">

    <property name="sessionFactory" ref="sessionFactory"/>

  </bean>

  <!-- 向userService注入dao -->

  <bean id="userService" class="service.UserServiceImpl">

    <property name="userDao" ref="userDao"></property>

  </bean>

  <!-- 定义事务管理器  -->

  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory"/>

  </bean>

  <!-- 配置事务增强 -->

  <tx:advice id="txAdvice" transaction-manager="txManager">

    <!-- 定义属性,声明事务规则 -->

    <tx:attributes>

      <tx:method name="find*" read-only="true"/>

      <tx:method name="search*" read-only="true"/>

      <tx:method name="query*" read-only="true"/>

      <tx:method name="add*" propagation="REQUIRED"/>

      <tx:method name="del*" propagation="REQUIRED"/>

      <tx:method name="update*" propagation="REQUIRED"/>

      <tx:method name="do*" propagation="REQUIRED"/>

      <tx:method name="*" propagation="REQUIRED" read-only="true"/>

    </tx:attributes>

  </tx:advice>

  <aop:config>

    <!-- 配置切入点 -->

    <aop:pointcut expression="execution(* service.*.*(..))" id="serviceMethod"/>

    <!-- 将事物增前和切入点结合 -->

    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>

  </aop:config>

</beans>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <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>

  <!-- OpenSessionInViewFilter,配置次功能可以延时加载,即保持会话随事件开启 -->

  <filter>

    <filter-name>OpenSessionInViewFilter</filter-name>

    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>OpenSessionInViewFilter</filter-name>

    <url-pattern>*.action</url-pattern>

  </filter-mapping>

</web-app>



spring整合hibernate的applicationContext.xml文件配置以及web.xml