Spring4.3入門 bean的作用域介紹

測試方法

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

 Persons pp = (Persons) ctx.getBean("huan");
 Persons pps = (Persons) ctx.getBean("huan");
 System.out.println(pp == pps)

  • singleton :容器初始化時創建實例,在整個容器證明週期中只創建一次 也是默認值
  <bean id="huan" autowire="byName" class="cn.nokia.pojo.Persons" p:age="23" p:name="erhuan" depends-on="car"
         ></bean>

結果 true

  • prototype : 原型的,容器初始化的時候不創建bean實例,每次請求的時候創建一個bean,並返回。
    <bean id="huan" autowire="byName" class="cn.nokia.pojo.Persons" p:age="23" p:name="erhuan" depends-on="car"
          scope="prototype"></bean>

結果 false

發佈了237 篇原創文章 · 獲贊 12 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章