春豆的生命周期是什么?
我对Spring的生命周期感到困惑。
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
上面的代码片段是否创建了对象?
如果上述答案是真的。
a)然后,对于范围为“singleton”的bean,获取在上面的代码片段中创建的对象。 我是对还是错?
b)对于范围是“原型”的情况,创建的对象是否未使用。 因为,容器总是返回新对象。
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
上面的代码片段是否创建了对象?
如果答案是假的,
spring框架如何validationbean定义是否正确。
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时你都会得到一个新的实例。 但是一旦在自动assembly期间使用了这样的bean,将永远使用相同的实例。
使用ApplicationContext
所有单例都是热切创建的,并且只在需要时创建原型bean。
也可以看看
- BeanFactory与ApplicationContext
通常,在上下文启动时会创建单例bean。 这可以使用lazy-init
或default-lazy-init
属性进行更改。
原型bean仅在需要时创建。