Tom Xu

           

          Spring 學習筆記(實例化方式 spring bean作用域 spring依賴注入 spring自動掃描 spring和struts整合步驟)

           
          spring bean 實例化的方式
                  1.默認情況下是調用bean對象的默認構造函數來示例話bean
                  2.調用工廠的靜態方法來實例化bean
                          <bean id="bean實例name" class="工廠類" factory-method="工廠的靜態方法"></bean>
                  3.調用工廠方法來實例化bean
                           <bean id="工廠實例name" class="工廠類"  class="工廠類名">
                           <bean id="bean實例name" factory-name="工廠實例name",factory-method="工廠的非靜態方法">
          spring bean 的作用域 (可以使用scope="singleton/prototype/request/session/global session"來配置)
                  1.默認作用域(singleton)情況下:
                  bean的實例化是在容器實例化(被主動申請加載)時就實例化
                  將lazy-init="true" (延遲初始化) 不會再容器實例化實例化bean (不建議使用)
                  用init-method="methodName" 可以指定bean被實例化時調用的方法
                  用destory-method="methodName" 可以制定容器調用close方法后bean執行的方法
                   2.配置prototype(原型)作用域:
                 bean的實例化是在調用getBean方法時被實例化的
                 每次調用一個getBean方法都回返回一個新的對象

          Spring Dependency Injection(Spring的依賴注入)
                 1.使用映射的方式注入
          <bean id="personDao" class="接口實現類路徑"></bean>   
          <bean id="persionService" class="接口實現的類路徑">
              <property name="personDao" ref="personDao"></property>   <!--反射賦值-->    
              <property name="name"  value="tom test!!!!!!!!"></property> <!--為基本類型賦值-->
                      <property name="name"  value="100"></property> <!--為基本類型賦值-->
          </bean>
                 2.使用內部bean 的方式注入
                  <bean id="persionService" class="實現的類路徑">
                       <property name="persionDao">
                              <bean  class="dao實現路徑"/>
                       </property>
                  </bean>
                3.集合類型的注入
                            <bean id="personService" class="實現類路徑">
                               <property name="sets">
                                  <set>                                                                    <!--此處可以為list-->
                                          <value>一個value</value>
                                          <value>兩個value</value>
                                  </set>
                                   <map>
                                           <entry key="key-1" value="value-1"/>
                                          <entry key="key-2" value="value-2"/>
            <entry key="key-3" value="value-3"/>
                                   </map>
                               </property>
                             </bean> 
                4.使用構造器參數注入(調用特定的構造函數)
                       如果PersonServiceBean中的有構造函數PersonServiceBean(PersonDao personDao,String name){}
                          <bean id="personDao11111111" class="類實現路徑"><bean>
                          <bean id="personService" class="類實現路徑">
                              <constructor-arg index="0" type="自定義type類的路徑" ref="personDao111111">
                              <constructor-arg index="1" type="基本類型可以省去......"  value="111111111">
                          </bean>       
                5.使用Field注入(用于注解的方式)
                  屬性的值注入
                  @Resource private PersonDao personDao;     /* 先找到 容器中的 bean的對應名稱進行匹配,若無,找類型匹配 */   
                   屬性的set方法注入
                  @Resource
                  public void setXXX(Object object){}     

          Spring 自動掃描和管理(掃描的方式去自動管理組建)
          引入類配置<context:component-scan base-package>
                           @Service @ Resposity
                    通過class 類名首字母小寫 @Service("對象名")來獲得,也可以  @scope("prototype") 來設置初始化類的屬性
                   可以在方法名上面@PostConstruct 可以制定初始化方法
                   可以在方法名上面@PreDestory 可以在bean實例被摧毀之前執行的方法。

          Spring和Struts2的整合
          1.找到Struts2-Spring-plugin-.XX.jar包和Spring.jar包 加入到web項目的lib包下
          2.配置WEB-INF/web.xml文件,加入與struts整合的監聽器
               <!-- 
             <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>classpath:beans.xml</param-value>
          </context-param>
               -->
              <!-- 若applicationContext.xml文件不在WEB-INF 下則需要將將路徑放入上面參數中-->
          <listener>
                     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
                  
          3.在struts.xml文件中加入spring常量(structs中對象的創建叫給spring)
               <constant name="struts.objectFactory" value="spring"/>    
              <action class="loginAction" name="loginAction_*"  method="{1}">
                    <result name="success" type="redirect">index.jsp</result> 
             </action>

          4.創建applicationContext.xml文件,文件可以放在web-inf目錄下,這樣可以不用在1中加入context的配置
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">           
              <bean id="loginAction" class="xxxxxxxxxxxxxxxxxxxxxxx" scope="prototype"/>
          </beans>
          注意:(1)struts2中需要加入prototype(原型)的配置,這樣才能滿足struts2為每次請求創建一個對象的服務方式  
                    (2)此處的id 名需要與3中的class同名

          posted on 2012-11-04 22:43 Tom Xu 閱讀(2376) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 泗洪县| 洪泽县| 博客| 仪陇县| 衡水市| 郯城县| 来凤县| 化德县| 淄博市| 江津市| 广德县| 定安县| 沾益县| 古交市| 尚志市| 莱西市| 青州市| 云和县| 焦作市| 闸北区| 丰城市| 富裕县| 屏东市| 绥江县| 阳江市| 三明市| 门源| 平遥县| 个旧市| 来宾市| 苍梧县| 高邮市| 榕江县| 来安县| 海南省| 遵化市| 孟州市| 丽江市| 青河县| 交城县| 西乌珠穆沁旗|