spring學習四

來源:互聯網
上載者:User

標籤:資訊   nfa   st表   get   狀態   tps   返回   spring執行個體   執行個體   

1: InitializingBean  vs  init-method

      InitializingBean 是一個介面,有一個方法afterPropertiesSet,不建議使用;因為InitializingBean是Spring介面,這樣導致 bean和spring耦合到一起了。

     <bean id="SimpleBean1" class="com.SimpleBean" init-method="init" destroy-method="cleanUp >        <property name="name" value="Bill"></property>        <property name="age" value="19"></property>      </bean> 

       DisposableBean介面 destroy() ,一般用destory-method替代

2:ApplicaitonContextAware ,   BeanNameAware介面, BeanFactoryAware 

      ApplicaitonContextAware :setApplicationContext()  可以讓bean擷取context資訊。

      BeanNameAware:  可以讓bean獲得bean的name.

      BeanFactoryAware :  bean感知beanfactory介面

3: Spring中注入map的key必須是String類型。

4:bean的生命週期: 

  (1) Spring執行個體化bean.

  (2)  利用依賴注入來配置bean中所有的屬性值。

  (3)  beanNameAware,   setBeanName

  (4)  BeanFactoryAware,   setBeanFacetory()

  (5)  ApplicationContextAware, setApplicationContext(), 當前ApplicationContext執行個體的引用。

  (6)  BeanPostProcessor,   posProcessBeforeINnitalzation()

  (7)  InitializingBean, posProcessAfterINitialization()

      到這為止,bean就可以被用了,

  (8) DisposableBean,  destroy()

  (9) destroy-method,  定義銷毀bean的方法。

5: bean的範圍:

       scope = "singleton"(預設)

       prototype: 每一次請求(將其注入到另一個bean中,或者以程式的方式調用容器的getBean()方法)都會產生一個新的bean執行個體,相當於一個new的操作。

   request:request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP request內有效,配置執行個體:    

<web-app>...<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener>...</web-app>  <bean id="role" class="spring.chapter2.maryGame.Role" scope="request"/> 

 

    session:  和request類似

   global session:

其中比較常用的是singleton和prototype兩種範圍。對於singleton範圍的Bean,每次請求該Bean都將獲得相同的執行個體。容器負責跟蹤Bean執行個體的狀態,負責維護Bean執行個體的生命週期行為;如果一個Bean被設定成prototype範圍,程式每次請求該id的Bean,Spring都會建立一個Bean執行個體,然後返回給程式。在這種情況下,Spring容器僅僅使用new 關鍵字建立Bean執行個體,一旦建立成功,容器不在跟蹤執行個體,也不會維護Bean執行個體的狀態。

 

 

spring學習四

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.