<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 3, comments - 15, trackbacks - 0, articles - 26
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    spring-framework-1.2.6-chapter3

    Posted on 2005-12-28 19:38 morcble的blog 閱讀(247) 評論(0)  編輯  收藏 所屬分類: Spring

    最基礎的包中有兩個是org.springframework.beans 和org.springframework.context .這兩個包提供了spring依賴注射功能的基礎。
    BeanFactory提供了高級的配置機制用于管理beans。
    ApplicationContext接口建立在BeanFactory接口之上(BeanFactory的子類),又添加了其它的功能,比如:更容易與Spring AOP功能結合,信息資源處理,事件傳播,建立ApplicationContext和父Contexts的機制,應用層特殊的context(如WebApplicationContext)。
    ApplicationContext是BeanFactory的擴展,所有BeanFactory的功能都能在ApplicationContext中實現。在建立的項目處于j2ee環境的時候最好選擇ApplicationContext;當你對內存的占有很關心的時候(如一個applet會關心每KB大小)可以選擇BeanFactory(占內存少)。
    BeanFactory 是為了訪問Spring bean container,可以把它看成事實上的container.一般簡單的BeanFactory的實現是org.springframework.beans.factory.xml.XmlBeanFactory。
    創建 BeanFatory的3個辦法如下 
    1。Resource res = new FileSystemResource("beans.xml");
       XmlBeanFactory factory = new XmlBeanFactory(res);
    2。ClassPathResource res = new ClassPathResource("beans.xml");
       XmlBeanFactory factory = new XmlBeanFactory(res);
    3。ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
          // of course, an ApplicationContext is just a BeanFactory
       BeanFactory factory = (BeanFactory) appContext;
       (注:public interface Resourceextends InputStreamSource,
            public interface InputStreamSource)
    多數用法中,用戶不需要編寫代碼初始化BeanFactory 或 ApplicationContext,因為Spring框架會自動完成。例如:在web層提供了代碼自動裝載
    ApplicationContext,作為正常的j2eeWEB應用程序開始的一部分。
     

     配置bean的XML幾種格式如下:
     (1)。通過構造函數創建bean,bean可以是javabean 也可以不是,也不用實現別的接口。
        <bean id="exampleBean" class="examples.ExampleBean"/>
        <bean name="anotherExample" class="examples.ExampleBeanTwo"/>
        如果你需要實現IOC機制,你的bean需要一個默認的空參數構造函數。
     (2)。通過靜態工廠函數創建bean.
       <bean id="exampleBean" class="examples.ExampleBean2"
         factory-method="createInstance"/>
          createInstance是examples.ExampleBean2的靜態工廠方法,用于生成exampleBean。
     (3).通過實例工廠方法創建bean.
     <!-- The factory bean, which contains a method called createInstance -->
      <bean id="myFactoryBean" class="...">
      ...
       </bean>
       <!-- The bean to be created via the factory bean -->
      <bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>


    To singleton or not to singleton
    <bean id="exampleBean"
    class="examples.ExampleBean" singleton="false"/>
    <bean name="yetAnotherExample"
    class="examples.ExampleBeanTwo" singleton="true"/>


    幾種依賴注射格式
    (1)setter注射

    <bean id="exampleBean" class="examples.ExampleBean">
    <property name="beanOne"><ref bean="anotherExampleBean"/></property>
    <property name="beanTwo"><ref bean="yetAnotherBean"/></property>
    <property name="integerProperty"><value>1</value></property>
    </bean>
    <bean id="anotherExampleBean" class="examples.AnotherBean"/>
    <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
    public class ExampleBean {
    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;

    public void setBeanOne(AnotherBean beanOne) {
    this.beanOne = beanOne;
    }
    public void setBeanTwo(YetAnotherBean beanTwo) {
    this.beanTwo = beanTwo;
    }
    public void setIntegerProperty(int i) {
    this.i = i;
    }
    }

    (2)構造函數注射
    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
    <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
    <constructor-arg type="int"><value>1</value></constructor-arg>
    </bean>
    <bean id="anotherExampleBean" class="examples.AnotherBean"/>
    <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
    public class ExampleBean {
    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;
    public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
    this.beanOne = anotherBean;
    this.beanTwo = yetAnotherBean;
    this.i = i;
    }
    }

    (3)靜態工廠注射
    <bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
    <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
    <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
    <constructor-arg><value>1</value></constructor-arg>
    </bean>
    <bean id="anotherExampleBean" class="examples.AnotherBean"/>
    <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

    public class ExampleBean {
     ...
     // a private constructor
       private ExampleBean(...) {
       ...
       }
     // a static factory method
     // the arguments to this method can be considered the dependencies of the bean that
     // is returned, regardless of how those arguments are actually used.
         public static ExampleBean createInstance(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
            ExampleBean eb = new ExampleBean(...);
           // some other operations
            ...
          return eb;
         }
    }


    構造函數配置的另一種格式
    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0"><value>7500000</value></constructor-arg>
    <constructor-arg index="1"><value>42</value></constructor-arg>
    </bean>
    可以在配置文件中加入驗證數據的類型 如下
    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" type="int"><value>7500000</value></constructor-arg>
    <constructor-arg index="1" type="java.lang.String"><value>42</value></constructor-arg>
    </bean>

    一般屬性配置格式
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- results in a setDriverClassName(String) call -->
      <property name="driverClassName">
         <value>com.mysql.jdbc.Driver</value>
      </property>
      <property name="url">
         <value>jdbc:mysql://localhost:3306/mydb</value>
      </property>
      <property name="username">
         <value>root</value>
      </property>
    </bean>

    注意:<value></value> 默認為空字符串,


    屬性null的格式如下
    <bean class="ExampleBean">
     <property name="email"><null/></property>
    </bean>


    屬性collection的格式如下
    <bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setPeople(java.util.Properties) call -->
    <property name="people">
       <props>
         <prop key="HarryPotter">The magic property</prop>
         <prop key="JerrySeinfeld">The funny property</prop>
       </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
       <list>
         <value>a list element followed by a reference</value>
         <ref bean="myDataSource"/>
       </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
       <map>
         <entry>
             <key><value>yup an entry</value></key>
             <value>just some string</value>
         </entry>
         <entry>
             <key><value>yup a ref</value></key>
             <ref bean="myDataSource"/>
         </entry>
      </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
     <property name="someSet">
       <set>
         <value>just some string</value>
         <ref bean="myDataSource"/>
       </set>
     </property>
    </bean>

    3.3.3.4. Inner bean definitions via nested bean elements
    <bean id="outer" class="...">
    <!-- Instead of using a reference to target, just use an inner bean -->
       <property name="target">
          <bean class="com.mycompany.PersonImpl">//嵌套的bean不需要id
             <property name="name"><value>Tony</value></property>
             <property name="age"><value>51</value></property>
          </bean>
       </property>
    </bean>

    復合屬性定義
    <bean id="foo" class="foo.Bar">
    <property name="fred.bob.sammy" value="123"/> 屬性fred下有屬性bob,bob屬性下有sammy 把sammy置為123。
    </bean>

    3.3.4. Method Injection(方法注射)
    <!-- a stateful bean deployed as a prototype (non-singleton) -->
    <bean id="singleShotHelper class="..." singleton="false">
    </bean>
    <!-- myBean uses singleShotHelper -->
    <bean id="myBean" class="...">
       <lookup-method name="createSingleShotHelper" bean="singleShotHelper"/>
       <property>
       ...
       </property>
    </bean>
    我的理解:myBean 中必須實現 protected [abstract] SingleShotHelper createSingleShotHelper();
    singleShotHelper中必須實現 protected  SingleShotHelper createSingleShotHelper();
    運行時  singleShotHelper的createSingleShotHelper會覆蓋myBean的createSingleShotHelper。//未驗證

    3.3.4.2. Arbitrary method replacement(任意的方法替換)
    public class MyValueCalculator {
         public String computeValue(String input) {
         ... some real code
         }
         ... some other methods
      }

    進行替換的類必須實現接口org.springframework.beans.factory.support.MethodReplacer
    public class ReplacementComputeValue implements MethodReplacer {
       public Object reimplement(Object o, Method m, Object[] args) throws Throwable {
      // get the input value, work with it, and return a computed result
       String input = (String) args[0];
       ...
       return ...;
      }
    }

    <bean id="myValueCalculator class="x.y.z.MyValueCalculator">
    <!-- arbitrary method replacement -->
        <replaced-method name="computeValue" replacer="replacementComputeValue">
           <arg-type>String</arg-type>     這里表示方法接受的參數,有幾個列幾個。
        </replaced-method>                 這樣myValueCalculator中的computeValue方法就被替換了
    </bean>
    <bean id="replacementComputeValue" class="a.b.c.ReplaceMentComputeValue"/>


    3.4.1生命周期
    <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
    public class ExampleBean {
       public void init() {
        // do some initialization work
       }
    }
    Is exactly the same as:
    <bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
      public class AnotherExampleBean implements InitializingBean {
      public void afterPropertiesSet() {
      // do some initialization work
      }
    }


    <bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
    public class ExampleBean {
        public void cleanup() {
       // do some destruction work (like closing connection)
       }
    }
    Is exactly the same as:
    <bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
        public class AnotherExampleBean implements DisposableBean {
        public void destroy() {
          // do some destruction work
        }
    }

     

    ===============================================
    3.7. 用BeanPostProcessors自定義beans
       ConfigurableBeanFactory bf = new .....; // create BeanFactory
        ... // now register some beans
       // now register any needed BeanPostProcessors
        MyBeanPostProcessor pp = new MyBeanPostProcessor();
       bf.addBeanPostProcessor(pp);
       // now start using the factory

    3.8. 用BeanFactoryPostProcessors 自定義bean factories
       XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
         // create placeholderconfigurer to bring in some property
         // values from a Properties file
       PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
       cfg.setLocation(new FileSystemResource("jdbc.properties"));
       // now actually do the replacement
       cfg.postProcessBeanFactory(factory);
    (也可以聲明式定義
       <!-- property placeholder post-processor -->
            <bean id="placeholderConfig"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                 <property name="location" value="jdbc.properties"/>
            </bean>

    由上
    以下這個處于beanfactory容器里的數據源就按jdbc.properties里的配置了
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="${jdbc.driverClassName}"/>
      <property name="url" value="${jdbc.url}"/>
      <property name="username" value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
    </bean>

    其中jdbc.properties如下
    jdbc.driverClassName=org.hsqldb.jdbcDriver
    jdbc.url=jdbc:hsqldb:hsql://production:9002
    jdbc.username=sa
    jdbc.password=root


    3.8.2. The PropertyOverrideConfigurer

    3.19. Creating an ApplicationContext from a web application
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>(the listener cannot be used in Servlet 2.2 compatible
    containers  但是在Servlet2.4后都普遍采用它)

    <!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    -->

    主站蜘蛛池模板: 国产成人精品日本亚洲专区| 国精无码欧精品亚洲一区| 日韩免费高清一级毛片| 亚洲欧洲中文日韩久久AV乱码| 99在线热播精品免费99热| 亚洲av无码一区二区三区天堂古代 | 天天拍拍天天爽免费视频| 一区二区三区免费在线观看| 久久精品国产亚洲AV果冻传媒| 成人性生交大片免费看无遮挡| 中文字幕一区二区免费| 亚洲色欲色欲www| 亚洲综合精品香蕉久久网| 67194成是人免费无码| 中文在线免费观看| 亚洲狠狠成人综合网| 亚洲Av综合色区无码专区桃色 | 成**人免费一级毛片| 国产一级高青免费| 99久久国产亚洲综合精品| 亚洲AV无码精品无码麻豆| 四虎永久在线精品免费观看地址 | 亚洲高清视频免费| 一级片在线免费看| 亚洲熟妇AV一区二区三区宅男| 国产亚洲成av人片在线观看| 国产成人免费片在线观看| 67pao强力打造国产免费| 精品一区二区三区高清免费观看 | 亚洲美女又黄又爽在线观看| 在线观看人成视频免费| 午夜无码A级毛片免费视频| 一级白嫩美女毛片免费| 亚洲欧美成人综合久久久| 亚洲精品国产手机| 国产成人亚洲综合无码精品| 亚洲国产av无码精品| 日韩在线天堂免费观看| 免费看污成人午夜网站| 蜜臀AV免费一区二区三区| 久9热免费精品视频在线观看|