Spring整合Hibernate Struts1.x Struts2.x的配置文件

这是最近整理的Spring整合Hibernate Struts1.x Struts2.x的相关配置文件,好的话大家拿去用,有什么不妥的地方欢迎大家批评指正!

Spring + hibernate +struts

一、spring对持久化的支持
    1、Spring的数据访问设计思想(DAO、模板方法)
    2、数据源配置:
        方式一:Spring内置实现 DriverManagerDataSource
            <!--  DataSource__DriverManagerDateSource  -->
            <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
                <property name="driverClassName">
                    <value>com.mysql.jdbc.Driver</value>
                </property>
                <property name="url">
                    <value>jdbc:mysql://localhost:3306/test</value>
                </property>
                <property name="username">
                    <value>root</value>
                </property>
                <property name="password">
                    <value>jiaojun</value>
                </property>
            </bean>
                  
        方式二:DBCP提供的BasicDataSource
            <!--  DataSource__BasicDateSource -->
            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                <property name="driverClassName">
                    <value>com.mysql.jdbc.Driver</value>
                </property>
                <property name="url">
                    <value>jdbc:mysql://localhost:3306/test</value>
                </property>
                <property name="username">
                    <value>root</value>
                </property>
                <property name="password">
                    <value></value>
                </property>
            </bean>
           
        方式三:JNDI数据源
            JNDI数据源:(mysql 5,tomcat 5.5)
               step1: 在tomcat/conf/server.xml中:
                   <Resource name="jdbc/mydatasource" auth="Container" description="DB Connection"
                type="javax.sql.DataSource" username="root" password=""
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/test" maxActive="5" />
            step2: 在tomcat/conf/context.xml中:
                <ResourceLink name="mydatasource" global="jdbc/mydatasource" type="javax.sql.DataSourcer"/>
            step3: 在spring   applicationContext.xml中:
                <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                    <property name="jndiName">
                      <value>java:comp/env/jdbc/mydatasource</value>
                    </property>
                  </bean>
             
    3、JDBC支持:
            step1: 配置数据源
            step2: 配置JdbcTemplate
                <!--  JdbcTemplate  -->
                <bean id="jdbcTemplate"
                    class="org.springframework.jdbc.core.JdbcTemplate">
                    <property name="dataSource">
                        <ref bean="dataSource" />
                    </property>
                </bean>      
            step3:配置DAO
            <!--  DAO -->
                <bean id="orderDao" class="lab5.OrderDAOImpl">
                    <property name="jt"><ref bean="jdbcTemplate"/></property>
                </bean>
           
            注意:查询时,使用RowMapper(使用RowMapper定义如何将Result转换为对象)
                implements RowMapper
                     
    4、hibernate支持:
            step1:    配置数据源
            step2:    配置sessionfactory
                <!--  Session Factory -->
                <bean id="mySessionFactory"
                    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                    <property name="dataSource">
                        <ref bean="dataSource" />
                    </property>
                    <property name="mappingResources">
                        <list>
                            <value>lab6/Order.hbm.xml</value>
                        </list>
                    </property>
                    <property name="hibernateProperties">
                        <props>
                            <prop key="hibernate.dialect">
                                org.hibernate.dialect.MySQLDialect
                            </prop>
                            <prop key="hibernate.show_sql">true</prop>
                            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                        </props>
                    </property>
                </bean>
        step3:    配置DAO
            <!--  DAO -->
            <bean id="orderDao" class="lab6.OrderDAOHibernateImpl">
                <property name="sessionFactory">
                    <ref bean="mySessionFactory" />
                </property>
            </bean>
           
            注意:以上配置是要求dao 继承HibernateDaoSupport
                  
二、Spring对事务的支持:
    1、Spring事务机制
        声明式事务、事务管理器
    2、hibernate事务p72
        step1: 配置数据源
        step2:配置sessionfactory (同上)
        step3:配置事务管理器
            <!--  Transaction Manager -->
            <bean id="myTransactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                    <ref bean="mySessionFactory" />
                </property>
            </bean>
        step4:配置DAO
        step5:配置目标对象
        step6:创建事务服务代理
        <!--  Service -->
            <bean id="saleService"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="proxyInterfaces">
                <value>lab7.SaleService</value>
            </property>
            <property name="transactionManager">
                <ref bean="myTransactionManager" />
            </property>
            <property name="target">
                <ref bean="saleServiceTarget" />
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
       
        注意:
            事务属性描述格式:
            传播行为,隔离级别,只读事务(readonly),回滚规则
            在默认情况下,Spring的容器对于非受查异常(服务模块中抛出的非受查异常)
            ,会回滚事务。对于受查异常,会提交事务。
            如果即使发生了某种受查异常,也要回滚事务,可以用  “- 异常类型“来声明。
            同样,对于非受查异常,如果不要求回滚事务,可以用"+异常类型"来声明
           
    3、简化事务配置
        继承、自动代理
       
    4、使用标注来进行事务管理(可选)
        (1)引入相应命名空间
            <?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.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        (2)在配置文件中添加
        <!--   -->
            <tx:annotation-driven transaction-manager="myTransactionManager"/>
        (3)在service类中,使用@Transactional标记
            增: @Transactional(propagation=Propagation.REQUIRED) 
            查: @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
            改: @Transactional(propagation=Propagation.REQUIRED,rollbackFor=StockException.class)
       
    5、分布式事务管理
        步骤:
        step1 导入jotm相关库
        step2 编写carol.properties配置文件
        step3 建两个数据库
        step4 在spring配置文件中,配置jotm
            <bean id="jotm"
                class="org.springframework.transaction.jta.JotmFactoryBean" />
               
            <bean id="txManager"
                class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="userTransaction" ref="jotm" />
            </bean>

            分布式事务要求数据源封装为XADataSource
            <bean id="bankDS"
            class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
                destroy-method="shutdown">
                <property name="dataSource">
                    <bean class="org.enhydra.jdbc.standard.StandardXADataSource"
                        destroy-method="shutdown">
                        <property name="transactionManager" ref="jotm" />
                        <property name="driverName"
                            value="com.mysql.jdbc.Driver" />
                        <property name="url"
                            value="jdbc:mysql://localhost:3306/txdb1" />
                    </bean>
                </property>
                <property name="user" value="root" />
                <property name="password" value="1234" />
            </bean>

        有一个问题:程序执行完后,不会自行停止
       
三、Spring与struts 1整合:
        前提:
            必须在Web应用启动时,创建Spring的ApplicationContext实例
        方式:
            1、采用ContextLoaderListener来创建ApplicationContext:
            <!--  ContextLoaderListener  -->
                <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>
            2、采用ContextLoaderPlugIn来创建ApplicationContext
            <!--   ContextLoaderPlugIn  -->
                <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
                        <set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
                </plug-in>
        或者:
            通过listener装载spring应用上下文
           
方式一:通过Spring的ActionSupport类
    ActionSupport类:
        知道ApplicationContext的获得方式。
    步骤:
        1、Action直接继承ActionSupport
        2、使用ApplicationContext ctx = getWebApplicationContext();取得Spring上下文
        3、取得相应Bean
        注意:有可能需要替换commons-attributes-compiler.jar包。
        优点:
            简单
        缺点:
            耦合高
            违反IOC
            无法使用多方法的Action
       
方式二:通过Spring的DelegatingActionProxy类
    步骤:
        1、Action中,使用IOC获得服务
        2、配置struts-config.xml
        <!--   -->
            <action path="/somepath" type="org.springframework.web.struts.DelegatingActionProxy"/>
        3、在Spring配置文件中
        <!--   -->
            <bean name="/somepath" class="SomeAction">
                <property name="service"><ref bean=""/>
            </bean>
            注意,要用bean name命名。
            /somepath:Action的path
    优点:
        不使用Spring api编写 Action
        利用了IOC装配。
        可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,
        避免struts中Action的线程安全问题。
    缺点:
        struts配置文件中,所有path都映射到同一个代理类
       
方式三:通过Spring的DelegatingRequestProcessor类
    步骤:
        1、Action中,使用IOC获得服务
        2、配置struts-config.xml
        <!--   -->
             <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
        3、在Spring配置文件中
        <!--   -->
            <bean name="/somepath" class="SomeAction">
                <property name="service"><ref bean=""/>
            </bean>
小结:
        Spring与Struts整合方式只有两种:
        (1)由Spring容器来管理Action(方式二,方式三)
        (2)Action处于容器之外(方式一)
        注意:
            中文问题:
                设置过滤器,设置页面编码,数据库编码
               

struts集成spring的方式一:

1、加载AppliationContext:
    配置插件
    <!--   -->
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
                        <set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
    </plug-in>
    或者采用listener方式:
    <!--   -->
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring-config/applicationContext.xml
                </param-value>
            </context-param>
            <listener>
                <listener-class>
                    org.springframework.web.context.ContextLoaderListener
                </listener-class>
            </listener>
2、    让Action继承ActionSupport
3、  调用ActionSupport的方法getWebApplicationContext获得WebApplicationContext
        通过WebApplicatioContext获得被spring容器管理的Service

方式二:通过Spring的DelegatingActionProxy类
    步骤:
        1、Action中,使用IOC获得服务
        2、配置struts-config.xml
        <!--   -->
            <action path="/somepath" type="org.springframework.web.struts.DelegatingActionProxy"/>
        3、在Spring配置文件中
        <!--   -->
            <bean name="/somepath" class="SomeAction">
                <property name="service"><ref bean=""/>
            </bean>
            注意,要用bean name命名。
            /somepath:Action的path
    优点:
        不使用Spring api编写 Action
        利用了IOC装配。
        可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,
        避免struts中Action的线程安全问题。
    缺点:
        struts配置文件中,所有path都映射到同一个代理类
       
方式三:通过Spring的DelegatingRequestProcessor类
    步骤:
        1、Action中,使用IOC获得服务
        2、配置struts-config.xml
        <!--   -->
             <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
        3、在Spring配置文件中
        <!--   -->
            <bean name="/somepath" class="SomeAction">
                <property name="service"><ref bean=""/>
            </bean>
小结:
        Spring与Struts整合方式只有两种:
        (1)由Spring容器来管理Action(方式二,方式三)
        (2)Action处于容器之外(方式一)
        注意:
            中文问题:
                设置过滤器,设置页面编码,数据库编码
   
   
   
struts2集成spring的方式:
    1 加载AppliationContext:
    配置插件,在struts.xml
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
                        <set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
    </plug-in>
    或者采用listener方式(在web.xml):
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring-config/applicationContext.xml
                </param-value>
            </context-param>
            <listener>
                <listener-class>
                    org.springframework.web.context.ContextLoaderListener
                </listener-class>
            </listener>
       
    2 添加Action由spring创建
            <constant name="struts.objectFactory" value="spring" />
     注意: Action中的class是个 ”逻辑名称“ 要与application中bean 的id名一样


片段:
<struts>
    <constant name="struts.objectFactory" value="spring" />
    <package name="user" extends="struts-default">
            <action name="sendMsg" method="sendMsg" class="MsgAction">
            <result>/message.jsp</result>
            <result name="input">/message.jsp</result>
        </action>
    </package>
</struts>


3 实际处理的Action,scope="prototype" 一定要设定,该属性说明Action可以生成多个
   
    application中的片段:
        <bean id="UserAction" scope="prototype"
                class="action.UserAction">
               
        </bean>
4 hibernate整合与spring整合Hibernate相同   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值