Spring-BeanLifeCycle(Bean的生命周期)

  1. The Bean Container finds the definition of the Spring Bean in the Configuration file.
  2. 从配置文件中找到Bean的定义
    The Bean Container creates an instance of the Bean using Java Reflection API.
  3. 使用反射创建Bean的实例
    If any properties are mentioned, then they are also applied. If the property itself is a Bean, then it is resolved and set.
  4. 为创建的实例赋予他们预设的属性
    If the Bean class implements the BeanNameAware interface, then the setBeanName() method will be called by passing the name of the Bean.
  5. 如果Bean的类实现了BeanNameAware接口,其setBeanName()方法会被调用,传入此bean的名字
    If the Bean class implements the BeanClassLoaderAware interface, then the method setBeanClassLoader() method will be called by passing an instance of the ClassLoader object that loaded this bean.
  6. 如果Bean的类实现了BeanClassLoaderAware接口,其setBeanClassLoader() 方法会被调用,传入加载此bean的ClassLoader
    If the Bean class implements the BeanFactoryAware interface, then the method setBeanFactory() will be called by passing an instance of BeanFactory object.
  7. 同理传入BeanFactory
    If there are any BeanPostProcessors object associated with the BeanFactory that loaded the Bean, then the method postProcessBeforeInitialization() will be called even before the properties for the Bean are set.

If the Bean class implements the InitializingBean interface, then the method afterPropertiesSet() will be called once all the Bean properties defined in the Configuration file are set.
If the Bean definition in the Configuration file contains a 'init-method' attribute, then the value for the attribute will be resolved to a method name in the Bean class and that method will be called.
The postProcessAfterInitialization() method will be called if there are any Bean Post Processors attached for the Bean Factory object.
If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.

你可能感兴趣的:(Spring-BeanLifeCycle(Bean的生命周期))