單例模式&BeanFactory與 ApplicationContext之間的區別

來源:互聯網
上載者:User

標籤:證明   通知   手機   程式配置   集合屬性   過程   instance   int   標籤   

BeanFactory部落格書寫 使用對象的時候建立,這是和ApplicationContext的區別

        Resource resource=new ClassPathResource("applicationContext.xml");

BeanFactory ctx=new XmlBeanFactory(resource);

        擷取到上下文,並沒有初始化bean,等待使用bean的時候才初始化

  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

 

  所有的bean已經初始化了

 

  http://www.yesky.com/imagesnew/software/spring/beans.html

代碼通過getBean()方法從容器擷取指定的bean執行個體,容器首先會調用Bean類的無參構造器,建立執行個體對象

 

 

AOP概念:

   AOP面向切面編程,橫切關注點

 

2.AOP專業術語

切點:主業務類中能匹配到切點運算式的方法

切面:通知和切點的集合  交叉業務類稱為切面

連接點:主業務類中的方法

通知:增強類中的方法

織入:動態過程,將通知和切點綁定的過程稱為織入

 

基於xml的DI(Dependency Injection)

     注入類型:

       1.1設值注入(set方法注入):本質上是調用了Bean的setXXX()進行值的注入

 

       普通屬性

          <property name="name" value="微冷的雨"></property>

          <property name="age" value="18"></property>

 

       域屬性(JavaBean屬性)

       <property name="myCar" ref="mmCar"></property>

 

接下來到我們昨天學的了

構造注入,開始以為很難,其實也不過如此

12.  *構造注入:

         <bean id="stu2" class="cn.happy.day01.entity.Student">

<constructorb-arg> 是構造標籤

           <constructor-arg index="0" value="微冷的雨"></constructor-arg>  

           <constructor-arg index="1" value="18"></constructor-arg>  

           <constructor-arg index="2"  ref="mmCar"></constructor-arg>  

        </bean>

13.命名空間p注入 (2個步驟)

 

13.命名空間p注入 (2個步驟)

*********************************************************

      1.1 使用前要先要在Spring設定檔中引入p命名空間

       xmlns:p="http://www.springframework.org/schema/p"

 

      1.2.本質上還是set方法注入   

      <!--02.p命名空間注入 -->

      <bean id="stu3" class="cn.happy.day01.entity.Student"

      p:name="國慶放假買手機" p:age="18" p:myCar-ref="mmCar">

 

集合屬性注入

 

<bean id="array" class="cn.happy.spring04collection.MyCollection">

       <property name="array">

          <array>

             <value>zly</value>

             <value>lxl</value>

          </array>

       </property>

    </bean>

 

List

   設定檔

     <bean id="listCollection" class="cn.day00di_xml.BeanCollection">

        <property name="list">

            <list>

                <ref bean="car"></ref>

                <ref bean="car2"></ref>

            </list>

        </property>

       </bean>

 

 Map

       <!--集合屬性注入之           Map-->

      <bean id="mapCollection" class="cn.day00di_xml.BeanCollection">

          <property name="map">

              <map>

                  <entry key="Green">

                      <ref bean="car2"></ref>

                  </entry>

                  <entry key="Pink">

 

  

     <!-- properties -->

    <bean id="properties" class="cn.happy.spring04collection.MyCollection">

       <property name="properties">

和Map集合屬性打出來的效果是一樣的

          <props>

            <prop key="001">001</prop>

            <prop key="002">002</prop>

          </props>

       </property>

    </bean>

 

1.1引入

    spring-aop-4.2.0.RELEASE.jar

     引入aopjar包是因為註解用到了aop的內容

    相同點:

    兩者都是通過xml設定檔載入bean,ApplicationContext和BeanFacotry相比,提供了更多的擴充功能。

   不同點:

    BeanFactory是消極式載入,如果Bean的某一個屬性沒有注入,BeanFacotry載入後,直至第一次使用調用getBean方法才會拋出異常;而ApplicationContext則在初始化自身是檢驗,這樣有利於檢查所相依性屬性是否注入;所以通常情況下我們選擇使用ApplicationContext

component:組件

     一個bean,就是一個組件

     scan:瀏覽

開啟包掃描器

cn.happy.entity

 <context:component-scan base-package="路徑名"></context:component-scan>

作業:

 

  3.BeanFactory 和ApplicationContext區別

 

   3.一個簡單的例子來證明BeanFactory和ApplicationContext主要區別

    搭建工程的環境就不說了,直接上代碼。

1.首先建立一個實體類:User

 

public class User {     public User(){      System.out.println("執行個體化User");     }}

 

2.在建立一個ApplicationContext.xml檔案

<?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.xsd">    <bean id="user" class="cn.test.User"></bean></beans>

 

3.建立測試類別

   

public class TestHappy { /** * BeanFactory的測試 */@Testpublic void beanFactoryTest(){BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));}  /** * ApplicationContext的測試 */@Testpublic void applicationContextTest(){ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");} }

 

BeanFactory測試結果:

 

 

ApplicationContext測試結果:

 

       

 

  總結:

   BeanFactory當需要調用時讀取配置資訊,產生某個類的執行個體。如果讀入的Bean配置正確,則其他的配置中有錯誤也不會影響程式的運行。而ApplicationContext 在初始化時就把 xml 的配置資訊讀入記憶體,對 XML 檔案進行檢驗,如果設定檔沒有錯誤,就建立所有的Bean ,直接為應用程式服務。相對於基本的BeanFactory,ApplicationContext 唯一的不足是佔用記憶體空間。當應用程式配置Bean較多時,程式啟動較慢。

單例模式&BeanFactory與 ApplicationContext之間的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.