熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts 2, spring 2, hibernate

2022-06-13   來源: Java開源技術 

  struts 與 spring
   
    struts 本身就采用了類似於spring的IOC容器機制可以利用struts官方提供的插件strutsspringpluginjar直接與spring進行整合配置文件中只需要設置
    strutsobjectFactory=spring
    這樣就將struts的對象管理交給了spring的IOC容器
    在strutsxml中配置的action
    <package name=maintaince extends=strutsdefault>
    <action name=serverInfoList class=serverInfoService method=getAllServersInfo>
    <result name=list>/jsp/server_info/server_info_listjsp</result>
    </action>

  在spring的配置文件中配置的bean
    <bean id=serverInfoService class=comwodserviceimplServerInfoServiceImpl>
                <property name=serverInfoDao ref=serverInfoDao/>
                <property name=sib ref=serverInfoBean/>
             </bean>
                  可以看出struts可以直接只用在spring中配置的bean引用過來作為action
                  這樣struts就可以跑在spring裡面了

  另外在webxml中還有這些內容
                    <contextparam>
                <paramname>contextConfigLocation</paramname>
                <paramvalue>classpath:properties/workassistant*xml</paramvalue>
            </contextparam>
            加載spring的配置文件
            <listener>
            <listenerclass>orgsprntextContextLoaderListener</listenerclass>
            </listener>
            設置spring的context listener
            <filter>
                <filtername>struts</filtername>
                <filterclass>orgapachestrutsdispatcherFilterDispatcher</filterclass>
            </filter>
            <filtermapping>
                <filtername>struts</filtername>
                <urlpattern>/*</urlpattern>
            </filtermapping>
            設置struts的dispatcher

  hibernate 與 spring

  Spring 與 hibernate結合的時候配置文件修改比較多首先是hibernate的自身的配置被集成到了spring的配置文件中了

  配置datasource:
    <bean id=dataSource class=orgspringframeworkjdbcdatasourceDriverManagerDataSource
            p:driverClassName=${nnectiondriver_class}
            p:url=${nnectionurl}
            p:username=${nnectionusername}
        p:password=${nnectionpassword}/>


    .配置sessionFactory
    <bean id=sessionFactory
    class=orgspringframeworkormhibernateannotationAnnotationSessionFactoryBean scope=prototype>
            <property name=dataSource ref=dataSource />
            <property name=annotatedClasses><! or use <property name=annotatedPackages> >
                <list>
                    <value>comwodbeanApplication</value>
                </list>
            </property>
            <property name=hibernateProperties>
                <props>
                    <prop key=hibernatehbmddlauto>create</prop>
                    <prop key=hibernatedialect>${hibernatedialect}</prop>
                    <prop key=hibernateshow_sql>${hibernateshow_sql}</prop>
                    <prop key=hibernategenerate_statistics>${hibernategenerate_statistics}</prop>
                </props>
            </property>
            <property name=eventListeners>
                <map>
                    <entry key=merge>
    <bean class=orgspringframeworkormhibernatesupportIdTransferringMergeEventListener/>
                    </entry>
                </map>
            </property>
        </bean>
        這兩個bean是spring結合hibernate的最主要的兩個bean
        當這兩個bean設置好了之後就可以直接使用spring提供的HibernateDaoSupport 直接使用封裝好的hibernate特性非常方便
        <bean id=serverInfoDao class=comwoddbhibernateServerInfoDAO>
            <property name=sessionFactory ref=sessionFactory/>
        </bean>
        初始化一個DAO
    public List<ServerInfoBean> getAllServersInfo() {
            List<ServerInfoBean> find = getHibernateTemplate()loadAll(ServerInfoBeanclass);
            return find;
        }
        直接調用getHibernateTemplate()訪問數據庫
       
    三.Spring 事務的設置

  .設置transactionManager
        <bean id=transactionManager class=orgspringframeworkormhibernateHibernateTransactionManager
                p:sessionFactoryref=sessionFactory/>
        設置advice
        <tx:advice id=txAdvice transactionmanager=transactionManager>
            <tx:attributes>
                <tx:method name=find* readonly=true/>
                <tx:method name=get* readonly=true/>
                <tx:method name=list* readonly=true/>
                <tx:method name=* rollbackfor=Exception/>
            </tx:attributes>
     </tx:advice>
        .接下來設置AOP
    <aop:config>
           <aop:pointcut id=businessService expression=execution(* comhisoftdbhibernateimpl**()) />
            <aop:advisor adviceref=txAdvice pointcutref=businessService />
            <aop:aspect id=businessAspect ref=AOPTest>
                 <aop:before pointcutref=businessService method=before/>
                 <aop:afterreturning pointcutref=businessService method=after/>
            </aop:aspect>
    </aop:config>
    這個的意思是說當執行到comhisoftdbhibernateimpl這個包下面的任何類的任何方法而且不管參數是什麼也就是說這個包下面的所有方法調用了都要接受前面的transactionManager的管理

  AOP設置
    <bean id=AOPTest class=monLogHandlerAOPTest/>
    <aop:config>
            <aop:pointcut id=businessService expression=execution(* comhisoftdbhibernateimpl**()) />
            <aop:advisor adviceref=txAdvice pointcutref=businessService />
            <aop:aspect id=businessAspect ref=AOPTest>
                 <aop:before pointcutref=businessService method=before/>
                 <aop:afterreturning pointcutref=businessService method=after/>
            </aop:aspect>
    </aop:config>

  定義一個切面叫做businessAspect引用的是我前面定義的一個叫做AOPTest的類然後下面的兩句話
            <aop:before pointcutref=businessService method=before/>
              <aop:afterreturning pointcutref=businessService method=after/>
        aop:before 指的是在調用目標方法之前要干點事情pointcutref=businessService就是目標的方法在調用匹配這個pointcut 的方法之前會調用 method中定義的那個方法


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28174.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.