Spring bean学习整理

Bean Life Cycle

2 important bean lifecycle callback methods:


1) initialization callbacks
2) Destruction callbacks


It is recommended that you do not use the InitializingBean or DisposableBean callbacks, because XML configuration gives much flexibility in terms of naming your method.

Default initialization and destroy methods:
If you have too many beans having initialization and or destroy methods with the same name, you don't need to declare init-method and destroy-method on each individual bean. Instead framework provides the flexibility to configure such situation using default-init-method and default-destroy-method attributes on the element as follows:

   
         
     
  

Bean Post Processors

the
BeanPostProcessor interface defines callback methods that you can implement to provide your own instantiation logic, dependency-resolution logic etc. After Spring container finishes instantiating, configuring and initializing a bean you can also implement some custom logic by plugging in one or more BeanPostProcessor implementations.

Set Order property to implement the Ordered interface.

An ApplicationContext automatically detects any beans that are defined with implementation of the BeanPostProcessor interface and registers these beans as post-processors, to be then called appropriately by the container upon bean creation.

Bean Definition Inheritance


Contain lots of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on.

Child definition can override some values or add others.

Spring Bean definition inheritance has nothing to do with Java class inheritance but inheritance concept is same.

Bean Definition Template:

You can create a Bean definition template which can be used by other child bean definitions without putting much effort. While defining a Bean Definition Template, you should not specify class attribute and should specify abstract attribute with a value of true as shown below:



 
   
           
   
 
 
  
    


The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions.

Dependency Injection
Dependency Injection (or sometime called wiring) helps in gluing these classes together and same time keeping them independent

Injecting Inner Beans
inner beans are beans that are defined within the scope of another bean. Thus, a element inside the or elements is called inner bean and it is shown below.

你可能感兴趣的:(Spring bean学习整理)