国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Struts 2, spring 2, hibernate 的整合-Java頻道-中國IT...
 一.struts 2 與 spring2
   
    struts2 本身就采用了類似于spring的IOC容器機(jī)制,可以利用struts2官方提供的插件struts2-spring-plugin-2.0.11.1.jar,直接與spring進(jìn)行整合,配置文件中只需要設(shè)置
    struts.objectFactory=spring
    這樣,就將struts2的對象管理交給了spring2的IOC容器。
    在struts.xml中配置的action
    <package name="maintaince" extends="struts-default">
    <action name="serverInfoList" class="serverInfoService" method="getAllServersInfo">
    <result name="list">/jsp/server_info/server_info_list.jsp</result>
    </action>

    在spring的配置文件中配置的bean
    <bean id="serverInfoService" class="com.wod.service.impl.ServerInfoServiceImpl">
                <property name="serverInfoDao" ref="serverInfoDao"/>
                <property name="sib" ref="serverInfoBean"/>
             </bean>
                  可以看出,struts2可以直接只用在spring2中配置的bean,引用過來作為action。
                  這樣struts2就可以跑在spring2里面了.

                  另外,在web.xml中還有這些內(nèi)容:
                    <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:properties/work-assistant*.xml</param-value>
            </context-param>
            加載spring的配置文件
            <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
            設(shè)置spring的context listener
            <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>
            設(shè)置struts2的dispatcher。

    二.hibernate3 與 spring2

    Spring 與 hibernate結(jié)合的時(shí)候,配置文件修改比較多。首先是hibernate的自身的配置被集成到了spring的配置文件中了.

    1.配置datasource:
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            p:driverClassName="${hibernate.connection.driver_class}"
            p:url="${hibernate.connection.url}"
            p:username="${hibernate.connection.username}"
        p:password="${hibernate.connection.password}"/>
2.配置sessionFactory
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" scope="prototype">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses"><!-- or use <property name="annotatedPackages"> -->
                <list>
                    <value>com.wod.bean.Application</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
                </props>
            </property>
            <property name="eventListeners">
                <map>
                    <entry key="merge">
    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                    </entry>
                </map>
            </property>
        </bean>
        這兩個(gè)bean是spring結(jié)合hibernate的最主要的兩個(gè)bean.
        當(dāng)這兩個(gè)bean設(shè)置好了之后,就可以直接使用spring提供的”HibernateDaoSupport” ,直接使用封裝好的hibernate特性,非常方便.
        <bean id="serverInfoDao" class="com.wod.db.hibernate.ServerInfoDAO">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        初始化一個(gè)DAO.
    public List<ServerInfoBean> getAllServersInfo() {
            List<ServerInfoBean> find = getHibernateTemplate().loadAll(ServerInfoBean.class);
            return find;
        }
        直接調(diào)用getHibernateTemplate()訪問數(shù)據(jù)庫。
       
    三.Spring 事務(wù)的設(shè)置

 

        1.設(shè)置transactionManager
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
                p:sessionFactory-ref="sessionFactory"/>
        2.設(shè)置advice
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="list*" read-only="true"/>
                <tx:method name="*" rollback-for="Exception"/>
            </tx:attributes>
     </tx:advice>
        3.接下來設(shè)置AOP
    <aop:config>
           <aop:pointcut id="businessService" expression="execution(* com.hisoft.db.hibernate.impl.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
            <aop:aspect id="businessAspect" ref="AOPTest">
                 <aop:before pointcut-ref="businessService" method="before"/>
                 <aop:after-returning pointcut-ref="businessService" method="after"/>
            </aop:aspect>
    </aop:config>
    這個(gè)的意思是說,當(dāng)執(zhí)行到com.hisoft.db.hibernate.impl這個(gè)包下面的任何類的任何方法,而且不管參數(shù)是什么,也就是說這個(gè)包下面的所有方法調(diào)用了,都要接受前面的transactionManager的管理。

        4.AOP設(shè)置
    <bean id="AOPTest" class="com.wod.common.LogHandler.AOPTest"/>
    <aop:config>
            <aop:pointcut id="businessService" expression="execution(* com.hisoft.db.hibernate.impl.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
            <aop:aspect id="businessAspect" ref="AOPTest">
                 <aop:before pointcut-ref="businessService" method="before"/>
                 <aop:after-returning pointcut-ref="businessService" method="after"/>
            </aop:aspect>
    </aop:config>

    定義一個(gè)切面,叫做businessAspect,引用的是我前面定義的一個(gè)叫做AOPTest的類,然后下面的兩句話:
            <aop:before pointcut-ref="businessService" method="before"/>
              <aop:after-returning pointcut-ref="businessService" method="after"/>
        aop:before 指的是在調(diào)用目標(biāo)方法之前要干點(diǎn)事情,pointcut-ref="businessService"就是目標(biāo)的方法,在調(diào)用匹配這個(gè)pointcut 的方法之前,會(huì)調(diào)用 method中定義的那個(gè)方法。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
淺析Spring 2.X中AOP的使用
spring面試題
Spring AOP原理及攔截器
【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我學(xué)spring3
簡單理解Spring之IOC和AOP及代碼示例
DispatcherServlet 和 ContextLoaderListener 的關(guān)系,到底用哪個(gè)?
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服