SSH 整合配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <display-name>Spring_2900_Registration_7</display-name>

    <welcome-file-list>
        <welcome-file>register.jsp</welcome-file>
    </welcome-file-list>

    <!-- 啟動Web容器時,自動裝配 Spring applicationContext.xml的配置信息,這里為 beans.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        <!-- default: /WEB-INF/applicationContext.xml -->
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
        <param-value>classpath:beans.xml</param-value>
    </context-param>

    <!-- Spring的OpenSessionInView實現(xiàn) -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <!-- singleSession默認(rèn)為true,若設(shè)為false則等于沒用OpenSessionInView 。所以默認(rèn)可以不寫 -->
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Struts 2 配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

beans.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <!-- 開啟注解處理器 -->
    <context:annotation-config />
    
    <!-- 開啟組件自動掃描,掃描路徑由 base-package 屬性指定 -->
    <context:component-scan base-package="com.bjsxt" />

    <!-- 定義數(shù)據(jù)源方式 1 -->
    <!-- 
    <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/spring" />
        <property name="username" value="root" />
        <property name="password" value="bjsxt" /> 
    </bean>
    -->

    <!-- 定義數(shù)據(jù)源方式 2 -->
    
    <!-- 設(shè)置數(shù)據(jù)庫連接配置文件 jdbc.properties -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <!-- 根據(jù)配置文件 jdbc.properties,設(shè)定數(shù)據(jù)源 -->
    <bean id="dataSource" destroy-method="close"    class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 定義Hibernate的SessionFactory -->
    <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <!-- 依賴注入上面定義的數(shù)據(jù)源dataSource -->
        <property name="dataSource" ref="dataSource" />
        
        <!-- 注冊 Hibernate 的ORM映射-->
        <!-- XML 方式 -->
        <!--  
        <property name="mappingResources">  
            <list>  
                <value>com/eportal/ORM/News.hbm.xml</value>  
                <value>com/eportal/ORM/Category.hbm.xml</value>  
            </list>  
        </property> 
        -->
        
        <!-- annotation 方式 -->
        <!-- 設(shè)定映射類的方式 -->
        <!-- 
        <property name="annotatedClasses">
            <list>
                <value>com.bjsxt.model.User</value> 
                <value>com.bjsxt.model.Log</value>
            </list>
        </property>
        -->
        <!-- 指定包掃描方式 -->
        <property name="packagesToScan">
            <list>
                <value>com.bjsxt.entity</value>
            </list>
        </property>
        <!-- 設(shè)置Hibernate的相關(guān)屬性 -->  
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- 使用 HibernateTemplate 將持久層訪問模板化 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--定義Hibernate的事務(wù)管理器HibernateTransactionManager -->  
    <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- 定義需要進行事務(wù)攔截的方法及所采用的事務(wù)控制類型 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="exists" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <!-- 使用 AOP 技術(shù)實現(xiàn)事務(wù)管理 -->
    <aop:config>
        <aop:pointcut id="bussinessService"     expression="execution(public * com.bjsxt.service.*.*(..))" />
        <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
    </aop:config>
</beans>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容