Believe it,do it!

          Ideal is the beacon. Without ideal, there is no secure direction; without direction ,there is no life.
          理想是指路明燈。沒有理想,就沒有堅定的方向;沒有方向,就沒有生活。
          CTRL+T eclipse
          posts - 35, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          struts hibernate spring整合總結

          Posted on 2009-02-11 17:51 三羽 閱讀(333) 評論(0)  編輯  收藏 所屬分類: JAVA資料

          1  Struts+Spring

          主要就是讓Struts知道Spring的存在,我們可以采用兩種辦法。

          (1) 運用Struts的插件方法,添加struts-congif.xml的<plug-in>節點。

          示例代碼如下:

          1<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">   
          2        <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />   
          3    </plug-in>  

          上段代碼意圖很簡單:把Struts的配置文件和Spring的配置文件聯系起來,這樣Struts就知道了Spring的存在。

          另外,在web.xml里面需要配置一個Spring的監聽器,示例代碼如下:

          1<listener>   
          2        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
          3    </listener> 

          第一種方法完畢。

          (2) 直接在web.xml里面進行加載。

          這種方法與上種方法的不同之處在于Spring的配置文件加載的位置不同,上種方法是在struts-cong.xml中聲明,而這種方法是在web.xml里面進行聲明,示例代碼如下:

          1<context-param>   
          2        <param-name>contextConfigLocation</param-name>   
          3        <param-value>   
          4            /WEB-INF/applicationContext-module1.xml,    
          5            /WEB-INF/applicationContext-module2.xml   
          6        </param-value>   
          7    </context-param> 

          這樣我們就實現了第二種方法的SS整合。

          問題至于我們還要遇到一個小問題,那就是Struts如何實用Spring的特性?Struts怎么就知道Spring注入的Bean呢?當然,這需要我們進行編程式的聲明,眾所周知,在Strus的Action里面有一個setServlet方法,我們此時就需要覆蓋這個方法,當ActionServlet把請求轉發到此Action時,Action就可以實用Spring的特性。

          示例代碼如下:

           1import org.springframework.context.ApplicationContext;    
           2import org.springframework.web.context.support.WebApplicationContextUtils;    
           3   
           4public void setServlet(ActionServlet actionServlet) {    
           5        try {    
           6            super.setServlet(actionServlet);    
           7            ServletContext servletContext = actionServlet.getServletContext();    
           8            context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
           9        }
           catch(Exception e) {    
          10            e.printStackTrace();    
          11        }
              
          12    }
             
          其中context是ApplicationContext的實例,有了上串代碼以后我們就可以以下面的方式進行Spring的特性利用了!
          1protected IXjdjService getXjdjServiceImp(){    
          2        return (IXjdjService) context.getBean("xjdj");    
          3    }
             

          到此OK,Struts與Spring的整合成功。

          2 Spring+Hibernate

          可以說Spring與Hibernate的整合是非常完善的,具體要素如下:

          在Hibernate中,最重要的無非就是兩點,一是配置數據庫連接池,二是配置實體類的映射文件。現在我貼出上述兩點在Spring里面是怎么配的,配置完畢以后,我們完全可以刪除hibernate.cfg.xml文件。示例代碼如下:

           1<bean id="dataSource"      
           2        class="org.springframework.jdbc.datasource.DriverManagerDataSource">      
           3        <property name="driverClassName">      
           4            <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>      
           5        </property>      
           6        <property name="url">      
           7            <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=EXAM</value>      
           8        </property>      
           9        <property name="username">      
          10            <value>sa</value>      
          11        </property>      
          12        <property name="password">      
          13            <value>135780</value>      
          14        </property>      
          15    </bean>      
          16    <!-- 配置Hibernate會話工廠 -->      
          17    <bean id="sessionFactory"      
          18        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">      
          19        <property name="dataSource">      
          20            <ref bean="dataSource" />      
          21        </property>      
          22        <!-- 在此可以配置Hibernate會話工廠的屬性 -->      
          23        <property name="hibernateProperties">      
          24            <props>      
          25                <prop key="hibernate.dialect">      
          26                    org.hibernate.dialect.SQLServerDialect       
          27                </prop>      
          28                <prop key="hibernate.show_sql">true</prop>      
          29                      
          30             </props>      
          31        </property>      
          32        <!-- 在此可以匹配*.hbm.xml映射文件 -->      
          33        <property name="mappingDirectoryLocations">      
          34            <list>      
          35                <value>      
          36                    classpath:/com/wjh/comm/framework/bean       
          37                </value>      
          38                    
          39            </list>      
          40        </property>      
          41        <property name="mappingResources">   
          42           <list>   
          43                <value>com/wjh/example/User.hbm.xml</value>   
          44          </list>   
          45 </property>   
          46</bean>   

          OK,到此就完成了Spring+Hibernate的整合

          主要功能:完成數據庫表的查、增、刪、改操作。
          問題描述:查詢功能一切正常,但是在增、刪、改操作中發現,數據庫表中數據沒有改變,查看控制臺日志也沒有任何異常發現,納悶了好久,仔查檢查日志,發現在新增操作中表id有在自增,由此確定是問題出在事務沒有提交。將事務配置上去后出現如下報錯:java.lang.ClassCastException: $Proxy1,百度上搜到一文,在業務類如果實現了接口,得增加如下代碼
          <property name="proxyTargetClass">
             <value>true</value>
          </property>
          果然如此,再次運行,成功了!
          以下是我applicationContext.xml的內容:
           1<?xml version="1.0" encoding="UTF-8"?>   
           2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
           3<beans>   
           4 <bean id="mySessionFactory"  
           5  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
           6  <property name="configLocation">   
           7   <value>classpath:hibernate.cfg.xml</value>   
           8  </property>   
           9 </bean>   
          10 <bean id="myTransactionManager"  
          11  class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
          12  <property name="sessionFactory">   
          13   <ref bean="mySessionFactory" />   
          14  </property>   
          15 </bean>   
          16 <bean id="myBaseTransactionProxy"  
          17  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
          18  abstract="true">   
          19  <property name="transactionManager" ref="myTransactionManager" />   
          20  <property name="transactionAttributes">   
          21   <props>   
          22    <prop key="*">PROPAGATION_REQUIRED</prop>   
          23    <!--    
          24     <prop key="insert*">PROPAGATION_REQUIRED</prop>   
          25     <prop key="save">PROPAGATION_REQUIRED</prop>   
          26     <prop key="update*">PROPAGATION_REQUIRED</prop>   
          27     <prop key="edit*">PROPAGATION_REQUIRED</prop>   
          28     <prop key="del*">PROPAGATION_REQUIRED</prop>   
          29     <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>   
          30     <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>   
          31     <prop key="disPlay*">PROPAGATION_REQUIRES_NEW</prop>   
          32    -->   
          33   </props>   
          34  </property>   
          35 </bean>   
          36 <bean id="UsersDAO" class="com.notepad.dao.UsersDAO">   
          37  <property name="sessionFactory">   
          38   <ref bean="mySessionFactory" />   
          39  </property>   
          40 </bean>   
          41 <bean id="userTarget" class="com.notepad.bussies.UserService">   
          42  <property name="usersDao">   
          43   <ref local="UsersDAO" />   
          44  </property>   
          45 </bean>   
          46 <bean id="UserService" parent="myBaseTransactionProxy">   
          47  <property name="proxyTargetClass">   
          48   <value>true</value>   
          49  </property>   
          50  <property name="target">   
          51   <ref local="userTarget" />   
          52  </property>   
          53 </bean>   
          54</beans>  
          55
          主站蜘蛛池模板: 滁州市| 锡林浩特市| 广汉市| 兴安县| 克什克腾旗| 墨竹工卡县| 吴堡县| 无棣县| 阿城市| 舞钢市| 长治县| 镶黄旗| 永济市| 石楼县| 平遥县| 迁西县| 遵化市| 木里| 南木林县| 灵宝市| 伊金霍洛旗| 连南| 巴塘县| 霍山县| 台南县| 临湘市| 滦平县| 盘山县| 易门县| 青田县| 凤山县| 西和县| 淮安市| 宜城市| 东乌| 喀喇| 临漳县| 卢龙县| 武川县| 大洼县| 鄂尔多斯市|