1, bean life cycle:
bean creation --- ---- initialization process of the destruction of
container-managed bean life cycle;
we can customize the initialization and destruction methods; in the bean container to the current life cycle time to call us from initialization and destruction methods defined.
2, specify the initialization and destruction methods;
Specifies the init-method and destroy-method by @Bean;
Initialization: After the object is created, and assigned a good initialization method is called (single instance or multi-instance);
destruction methods: single-instance: when the object is destroyed or closed container calls this method;
multi-instance: the container does not manage bean, so it will not call the destruction methods.
@Component public class Car { public Car(){ System.out.println("car constructor..."); } public void init(){ System.out.println("car ... init..."); } public void detory(){ System.out.println("car ... detory..."); } }
In the bean to a container:
@Configuration public class MainConfigOfLifeCycle { //@Scope("prototype") @Bean(initMethod="init",destroyMethod="detory") public Car car(){ return new Car(); } }
3, achieved by having the InitializingBean Bean (defined initialization logic) ------ afterPropertiesSet () method,
The DisposableBean (defined logical destruction) --- the destroy () method;
@Component public class Cat implements InitializingBean,DisposableBean { public Cat(){ System.out.println("cat constructor..."); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("cat...destroy..."); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("cat...afterPropertiesSet..."); } }
4, may be used JSR250;
@PostConstruct: Creating the bean is complete and the property assignment is completed; to perform initialization method
@PreDestroy: let us work to clean up before the destruction of bean container
@Component public class Dog { public Dog () { System.out.println ( "Dog constructor ..." ); } // After the object is created and assigned call @PostConstruct public void the init () { System.out.println ( " the PostConstruct @ .... ... Dog " ); } // container before removing the object @PreDestroy public void detory () { System.out.println ( " Dog The PreDestroy @ .... ... " ); } }
5, Spring provides us with BeanPostProcessor, bean post-processors
[BeanPostProcessor] interface: bean post-processor;
some initialization processing before and after the bean;
postProcessBeforeInitialization: work before initialization
postProcessAfterInitialization: after initialization work
/ ** * postprocessor: before and after the initialization processing * The postprocessor was added to the vessel * / @Component public class MyBeanPostProcessor the implements the BeanPostProcessor { @Override public Object postProcessBeforeInitialization (Object the bean, the beanName String) throws BeansException { / / the TODO Auto-Generated Stub Method System.out.println ( "postProcessBeforeInitialization ..." the beanName + + "=>" + the bean); return the bean; } @Override public Object postProcessAfterInitialization (Object the bean, the beanName String) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean); return bean; } }