Spring之配置文件bean作用域的詳細介紹


Spring的配置文件applicationContext.xml中bean作用域的詳細介紹:

1:對象的創建:單例和多例
        scope="singleton",默認值,單例 適合於【service,dao,工具類】
        scope="prototype",多例適合於【Action對象】
2:什么時候創建對象?
      scope="singleton" 在啟動的時候就已經創建了bean,且整個應用只有一個,在容器初始化之前
      scope="prototype" 在用到對象的時候才創建對象
3:是否延遲創建?(只對單例singleton有效,對多例無效):
       lazy-init="default" 默認是false,不延遲創建,即在啟動的時候就創建對象
       lazy-init="true" 延遲初始化,在用到對象的時候才創建
4:初始化和銷毀的方法:

  init-method="初始化方法名" 【對應對象的初始化方法,在對象創建之后執行】
      destroy-method="銷毀的方法名" 【在調用容器對象的銷毀方法的時候執行,容器必須使用實現類                                             ClassPathXmlApplicationContext,不能使用application接口】

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd">
11      
12      
13      <!-- IoC容器的配置,要創建的所有的對象都配置在這里 -->
14      <!-- <bean id="user" class="com.bie.po.User" scope="singleton"></bean> -->
15      <bean id="user" class="com.bie.po.User"></bean>
16      <!-- <bean id="user" class="com.bie.po.User" scope="prototype"></bean> -->
17      <bean id="user" class="com.bie.po.User" init-method=""></bean>
18      <bean id="user" class="com.bie.po.User" destroy-method=""></bean>
19      <bean id="user" class="com.bie.po.User" lazy-init="default"></bean>
20      
21 </beans>     

對於難啃的骨頭,我想說只能一點點啃了~.-.~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM