<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # ApplicationContext與BeanFactory關系 ![](https://box.kancloud.cn/45d079ad1b5eb3bf409e66a5dbefdc07_1026x970.png) ApplicationContext它是擴展BeanFactory接口。 **BeanFactory它采取延遲加載的方案,只有真正在getBean時才會實例化Bean** --- 在開發中我們一般使用的是ApplicationContext,真正使用的是其實現類, * FileSystemXmlAppliCationContext 根據文件路徑獲取 * ClassPathXmlApplicationContext 根據類路徑獲取 --- AppliCationContext它會在配置文件加載時,就會初始化Bean, 并且ApplicationContext它提供不同的應用層的Context實現。 例如在web開發中可以使用WebApplicationContext. # Bean實例化方式 ## 無參數構造 對于這種方式,注意Bean類中必須提供無參數構造 ~~~ <bean name="user" class="studySpring.User"></bean> ~~~ ## 靜態工廠方法 **是靜態方法創建** 需要創建一個工廠類,在工廠類中提供一個static返回bean對象的方法就可以 ~~~ <!-- 創建方式2:靜態工廠創建 調用UserFactory的createUser方法創建名為user2的對象.放入容器 --> <bean name="user2" class="hello.UserFactory" factory-method="createUser"></bean> ~~~ UserFactory ~~~ public static User createUser() { System.out.println("靜態工廠創建User"); return new User(); } ~~~ ## 實例工廠方法 **需要個實例,然后這個實例的方法創建** 需要創建一個工廠類,在工廠類中提供一個非static的創建bean對象的方法,在配置文件中需要將工廠配置,還需要配置bean ~~~ <!-- 創建方式3:實例工廠創建 調用UserFactory對象的createUser2方法創建名為user3的對象.放入容器 --> <bean name="UserFactory" class="hello.UserFactory"></bean> <bean name="user3" factory-bean="UserFactory" factory-method="createUser2"></bean> ~~~ UserFactory ~~~ public User createUser2() { System.out.println("實例工廠創建User"); return new User(); } ~~~ # FactoryBean接口 * **spring中有兩種類型的Bean,一種是普通的Bean,另一種是工廠Bean即FactoryBean** * 工廠Bean跟普通Bean不同,其返回的對象不是指定類的一個實例,其返回的是該工廠Bean的getObject方法所返回的對象 ~~~ public interface FactoryBean { //FactoryBean返回的實例 Object getObject() throws Exception; //FactoryBean返回的類型 class getObjectType(); //FactoryBean返回的實例是否為單例 boolean isSingleton(); } ~~~ 例子 FactoryBean類 ~~~ package com.spring; import org.springframework.beans.factory.FactoryBean; public class MyFactoryBean implements FactoryBean<User> { @Override public User getObject() throws Exception { return new User(); } @Override public Class<?> getObjectType() { return User.class; } @Override public boolean isSingleton() { return true; } } ~~~ xml ~~~ <!-- 通過FactoryBean配置Bean --> <bean id="myFactoryBean" class="com.spring.UserFactory"></bean> ~~~ # containsBean("xx")判斷容器中有沒有這個bean ~~~ boolean b = ctx.containsBean("u"); System.out.println(b); ~~~ # 內部bean 內部bean不能被其他bean引用,所以可以不指定id屬性值 ~~~ <!-- 配置內部bean --> <bean id="cartItem2" class="com.jdxia.spring.beans.CartItem"> <property name="book"> <!-- 配置內部bean --> <!-- 內部bean不能被其他bean引用,所以可以不指定id屬性值 --> <bean class="com.jdxia.spring.beans.Book"> <property name="id" value="6"></property> <property name="price" value="8.88"></property> <property name="sales" value="6666666"></property> </bean> </property> <property name="count" value="100"></property> <property name="amount" value="1000.00"></property> </bean> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看