Spring2.5+Hibernate3.2+Struts2.0+jotm2.0集成配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- spring listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-jta.xml</param-value>
    </context-param>

    <!-- spring log4j -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
    </context-param>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>webapp.root</param-value>
    </context-param>

    <!-- struts filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

struts.xml

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

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK" />
    <constant name="struts.objectFactory" value="spring" />
    <package name="useradmin" extends="struts-default">
        <action name="AddUser" class="addUserAction">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!-- data source -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
            <value>oracle.jdbc.driver.OracleDriver</value>
        </property>
        <property name="url">
            <value>jdbc:oracle:thin:@192.168.16.84:1521:develop</value>
        </property>
        <property name="username">
            <value>estarcom</value>
        </property>
        <property name="password">
            <value>estarcom</value>
        </property>
    </bean>

    <!-- hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="mappingResources">
            <list>
                <value>test/entity/User.hbm.xml</value>
                <value>test/entity/UserProfile.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!-- dao bean -->
    <bean id="userDao" class="test.dao.impl.UserDaoImpl">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <bean id="profileDao" class="test.dao.impl.UserProfileDaoImpl">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>

    <!-- service bean -->
    <bean id="userService" class="test.service.impl.UserServiceImpl">
        <property name="userDao">
            <ref local="userDao" />
        </property>
        <property name="profileDao">
            <ref local="profileDao" />
        </property>
    </bean>

    <!--struts action bean -->
    <bean id="addUserAction" class="test.action.AddUserAction" scope="prototype">
        <property name="userService">
            <ref local="userService" />
        </property>
    </bean>

    <!-- transaction manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>

    <!-- transaction control with aop -->
    <aop:config>
        <aop:pointcut id="serviceMethods" expression="execution(* test.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
</beans>

如果要使用jotm对多数据源进行JTA事务管理,配置文件如下:applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="userTransaction">
            <ref local="jotm" />
        </property>
    </bean>
    <bean id="oracleDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
        <property name="dataSource">
            <bean id="innerOracleDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
                <property name="transactionManager">
                    <ref local="jotm" />
                </property>
                <property name="driverName">
                    <value>oracle.jdbc.driver.OracleDriver</value>
                </property>
                <property name="url">
                    <value>jdbc:oracle:thin:@192.168.16.84:1521:develop</value>
                </property>
                <property name="user">
                    <value>estarcom</value>
                </property>
                <property name="password">
                    <value>estarcom</value>
                </property>
            </bean>
        </property>
        <property name="maxSize">
            <value>5</value>
        </property>
        <property name="minSize">
            <value>2</value>
        </property>
        <property name="user">
            <value>estarcom</value>
        </property>
        <property name="password">
            <value>estarcom</value>
        </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="oracleDataSource" />
        <property name="mappingResources">
            <list>
                <value>test/entity/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="jtaTransactionManager">
            <ref bean="jotm" />
        </property>
    </bean>
    <bean id="oracleDataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
        <property name="dataSource">
            <bean id="innerOracleDataSource2" class="org.enhydra.jdbc.standard.StandardXADataSource"
                destroy-method="shutdown">
                <property name="transactionManager">
                    <ref local="jotm" />
                </property>
                <property name="driverName">
                    <value>oracle.jdbc.driver.OracleDriver</value>
                </property>
                <property name="url">
                    <value>jdbc:oracle:thin:@192.168.16.84:1521:develop</value>
                </property>
                <property name="user">
                    <value>shmobile</value>
                </property>
                <property name="password">
                    <value>shmobile</value>
                </property>
            </bean>
        </property>
        <property name="maxSize">
            <value>5</value>
        </property>
        <property name="minSize">
            <value>2</value>
        </property>
        <property name="user">
            <value>shmobile</value>
        </property>
        <property name="password">
            <value>shmobile</value>
        </property>
    </bean>
    <bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="oracleDataSource2" />
        <property name="mappingResources">
            <list>
                <value>test/entity/UserProfile.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="jtaTransactionManager">
            <ref bean="jotm" />
        </property>
    </bean>

    <!-- dao bean -->
    <bean id="userDao" class="test.dao.impl.UserDaoImpl">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <bean id="profileDao" class="test.dao.impl.UserProfileDaoImpl">
        <property name="sessionFactory">
            <ref local="sessionFactory2" />
        </property>
    </bean>

    <!-- service bean -->
    <bean id="userService" class="test.service.impl.UserServiceImpl">
        <property name="userDao">
            <ref local="userDao" />
        </property>
        <property name="profileDao">
            <ref local="profileDao" />
        </property>
    </bean>

    <!--struts action bean -->
    <bean id="addUserAction" class="test.action.AddUserAction" scope="prototype">
        <property name="userService">
            <ref local="userService" />
        </property>
    </bean>

    <!-- transaction control with aop -->
    <aop:config>
        <aop:pointcut id="serviceMethods" expression="execution(* test.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
</beans>

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值