其他註解
@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
也是同理。