Spring → 04:Bean(1)

A, Bean concept

Spring Bean is instantiated, assembly and the Spring Java object container management.

Spring container will automatically @bean object instantiation.

Create a collaborative relationship between the application object's behavior is called: Assembly (wiring), this is the nature of dependency injection.

1.1, BeanFactory and factory pattern to create Bean

1.2, ApplicationContext container assembly and the main implementation class

Two, Bean basics

  2.1, the above mentioned id, name, class, scope attribute description

2.2, depends-on, abstract, parent property description

2.3, Spring bean initialization when

  1, if the scope of the bean is singleton, lazy-init to false (default) then the ApplicationContext; activated when bean instantiated, if true, then the first use of the bean when instantiated
  2, if the bean scope another type is instantiated in the first time using

2.4, Spring bean scopes

1, singleton singleton (default), singleton design pattern with a single mode of embodiment which is not the same as here, to ensure that the containers of this type there is only one shared bean instance.
2, prototy prototype, generating a new instance of each use
3, request is generated each time a new instance of the http request
4, session generate a new instance of each http session
. 5, each of the session global Global http session per bean

Three, Bean's life cycle

 

3.1, show various stages of the life cycle Bean

  1. instantiating a Bean, that is, we usually say that the new new
  2. configure the instance of the Spring Bean in accordance with the context, that is injected into the IOC
  3. If the Bean implements BeanNameAware interface calls setBeanName its implementation (String beanId ) method, passing here Spring configuration file Bean's ID. Implements this interface can obtain the current Bean's name.
  4. If the Bean implements BeanFactoryAware interface calls setBeanFactory its implementation (), passing the Spring plant itself (Bean can get to the other this way). Implement the interface can get BeanFactory object.
  5. If the Bean implements ApplicationContextAware interface calls setApplicationContext (ApplicationContext) method, passing in the Spring context, the ways to achieve the same Step 4, but better than 4, that ApplicationContext is a subinterface of BeanFactory, there is more to achieve method
  6. If this Bean associated with the BeanPostProcessor interface will be called postProcessBeforeInitialization (Object obj, String s) method, often used as a change BeanPostProcessor Bean content, and because this method is invoked at the end of Bean After initialization, can also be used in memory or cache.
  7. If the Bean configure the init-method property in the Spring configuration file will automatically call its initialization method configuration
  8. If the Bean associated with the BeanPostProcessor interface will be called postAfterInitialization (Object obj, String s) method
  Note: After completion of the above can use the Bean, and Bean that this is a single, so under normal circumstances we call the same Bean ID would be a same content address example
  9. Bean when no longer needed, will clean-up stage, if DisposableBean Bean implements the interface, will destroy method invoked its implementation
  10. Finally, if the Bean Spring configuration configure the destroy-method attributes, methods of destruction of its configuration will automatically call

3.2, custom analog Spring Bean container assembly of IoC

 

Guess you like

Origin www.cnblogs.com/BalmyLee/p/10936514.html