其他註解

其他註解

@Scope、@PreDestory、@PostConstruct

1. 用於指定作用範圍

@Scope

屬性:value

常用取值:

  • singleton:標識一個單例bean(默認值)
  • prototype:標識一個原型bean

2. 有關聲明週期的兩個註解

@PostConstruct,用於指定初始化時的回調方法

@PreDestroy,用於指定銷燬時的回調方法

在之前討論singeton和prototype兩種類型的bean時,其實已經使用到了上面的兩個回調方法,在哪裏使用的是基於XML的配置。

<bean name="singleton" 
    scope="singleton" 
    class="SpringTest.lifeCycle.Singleton"
    init-method="init"
    destroy-method="destroy"/>

在一個方法上使用@PreDestroy註解,就像當於destroy-method="name_of_function"@PostConstruct也是同理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章