对象生命周期<br>Bean lifecycle
创建对象实例。<br>1、Spring instantiates the bean.
向对象实例属性中注入基础值与对象引用。<br>2、Spring injects values and bean references into the bean's properties.
如果类实现了BeanNameAware接口,则调用setBeanName()函数注入实例ID。<br>3、If the bean implements BeanNameAware, Spring <br>passes the bean's ID to the set-BeanName() method.
如果类实现了BeanFactoryAware接口,则调用setBeanFactory()函数注入该对象的对象工厂实例。<br>4、If the bean implements BeanFactoryAware, Spring calls the setBeanFactory()<br>method, passing in the bean factory itself.
如果类实现了ApplicationContext接口,则调用setApplicationContext()函数注入该对象所属的应用上下文。<br>5、If the bean implements ApplicationContextAware, Spring calls the set-<br>ApplicationContext() method, passing in a reference to the enclosing application<br>context.
如果类实现了BeanPostProcessor接口,则调用postProcessBeforeInitialization()函数,执行自定义逻辑。<br>6、If the bean implements the BeanPostProcessor interface, Spring calls its post-<br>ProcessBeforeInitialization() method.
如果类实现了InitializingBean接口,则调用afterPropertiesSet()函数,同样的,如果类生命了初始化方法,初始化方法也会被调用。<br>7、If the bean implements the InitializingBean interface, Spring calls its after-<br>PropertiesSet() method. Similarly, if the bean was declared with an initmethod,<br>then the specified initialization method is called.
如果类实现了BeanPostProcessor接口,则调用postProcessAfterInitialization()函数。<br>8、If the bean implements BeanPostProcessor, Spring calls its postProcess-<br>AfterInitialization() method.
到这一步,对象已经可以被容器使用,并且已经存在上下文中了。<br>9、At this point, the bean is ready to be used by the application and remains in the<br>application context until the application context is destroyed.
如果类实现了DisposableBean接口,则调用destroy()函数。同样的,如果生命了销毁方法则调用该销毁方法。<br>10、If the bean implements the DisposableBean interface, Spring calls its<br>destroy() method. Likewise, if the bean was declared with a destroy-method,<br>the specified method is called.
对象生效范围<br>Bean scope
单例<br>Singleton
整个对象容器里面只有一个实例。<br>One instance of the bean is created for the entire application.
会话<br>Session
为每一个会话创建一个对象实例。<br>In a web application, one instance of the bean is created for each session.
请求<br>Request
为每一次请求创建一个对象实例。<br>In a web application, one instance of the bean is created for each request.
原型<br>Prototype
每一次获取或注入对象都创建新的对象实例。<br>One instance of the bean is created every time the bean is injected<br>into or retrieved from the Spring application context.