什么时候实例化一个spring bean [英] when is a spring bean instantiated
本文介绍了什么时候实例化一个spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer) ctx.getBean("duke");
performer.perform();
在上面,bean实例化的时间,创建ApplicationContext的时间或getBean()的时间被调用?
In the above, when are the beans instantiated, when the ApplicationContext is created or when the getBean() is called?
推荐答案
假设bean是单例,并且没有配置为延迟初始化,那么它是在上下文时创建的启动了。 getBean()
只是把它搞砸了。
Assuming the bean is a singleton, and isn't configured for lazy initialisation, then it's created when the context is started up. getBean()
just fishes it out.
Lazy-init bean只会在第一次引用时初始化,但是这个不是默认值。只有在第一次引用时才会创建范围内的bean(例如原型范围)。
Lazy-init beans will only be initialised when first referenced, but this is not the default. Scoped beans (e.g. prototype-scoped) will also only be created when first referenced.
这篇关于什么时候实例化一个spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文