![](/img/trans.png)
[英]How to tie the Lifecycle for a Spring Bean to the webapps' lifecycle?
[英]What is the lifecycle of spring bean?
我對Spring的生命周期感到困惑。
XmlBeanFactory beanFactory
= new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
上面的代碼片段是否創建了對象?
如果上述答案是真的。
a)然后,對於范圍為“singleton”的bean,獲取在上面的代碼片段中創建的對象。 我是對還是錯?
b)對於范圍是“原型”的情況,創建的對象是否未使用。 因為,容器總是返回新對象。
XmlBeanFactory beanFactory
= new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
上面的代碼片段是否創建了對象?
如果答案是假的,
spring框架如何驗證bean定義是否正確。
From the answer of Henry
Usually, singleton beans are created when the context starts. This can be changed with the lazy-init or default-lazy-init attributes.
Prototype beans are only created when needed.
Only syntactically, there might still be errors when the bean is instantiated, for example if a required property is not provided.
BeanFactory
不像ApplicationContext
那樣在啟動時預先實例化單例。 所以,即使你的bean是非懶惰和單身,它也不會被創建。
prototype
bean是按需創建的,每次你要求原型bean時你都會得到一個新實例。 但是一旦在自動裝配期間使用了這樣的bean,將永遠使用相同的實例。
使用ApplicationContext
所有單例都是熱切創建的,並且只在需要時創建bean。
通常,在上下文啟動時會創建單例bean。 這可以使用lazy-init
或default-lazy-init
屬性進行更改。
原型bean僅在需要時創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.