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="oracle.jdbc.driver.OracleDriver"/>

    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>

    <property name="username" value="james"/>

    <property name="password" value="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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章