spring - bean lifecycle - resolution order, registerShutdownHook ()

1, summary

Here Insert Picture Description
Among them, @PostConstructthe call and BeanPostProcessorrelevant.

He stressed:
the need to make appropriate decisions based on application requirements:

  • When you need to consider the portability issue, use the callback method
  • When the need to reduce the amount of configuration, the interface may be used DisposableBean
  • (I did not write notes scenario of the book, notes seem to fit the needs of two or more)

2, the test

Test class: A

package book.chapter4;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class A implements InitializingBean , DisposableBean  {
	public A() {
		System.out.println("1. construct ... ");
	}
	
	private String a ;

	public String getA() {
		return a;
	}

	public void setA(String a) {
		System.out.println("2. setting ... ");
		this.a = a;
	} 
	
	@PostConstruct
	public void postConstruct() {
		System.out.println("3. postConstruct ... ");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("4. afterPropertiesSet.afterPropertiesSet() ... ");
	}
	
	public void init() {
		System.out.println("5. init-method ... ");
	}
	public void doSomething() {
		System.out.println("-------------- do something ... ");
	}

	@PreDestroy
	public void PreDestroy() {
		System.out.println("1. PreDestroy ... ");
	}
	
	@Override
	public void destroy() throws Exception {
		System.out.println("2. DisposableBean.destroy() ... ");
	}
	
	public void destroyMethod() {
		System.out.println("3. destroy-method ... ");
	}
	
}

Test categories: Demo

package book.chapter4;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.GenericXmlApplicationContext;

public class BeanDemo {
	
	@Test
	public void testXml() {
		System.out.println("###### load  ##############");
		GenericXmlApplicationContext gtx = new GenericXmlApplicationContext();
		gtx.load("classpath:book/chapter4/app-context-a.xml") ;
		gtx.refresh();
		
		System.out.println("###### getBean ############");
		A bean = gtx.getBean("a" , A.class);
		bean.doSomething();
		
		System.out.println("###### close  #############");
		gtx.close();
	}
	
}

Test Results

Here Insert Picture Description

3, extension

The only drawback discard callback in Spring is they do not automatically trigger;

If the implementation applicationContext is a good solution when GenericXmlApplicationContext

registerShutdownHook()

Only need to register for destruction method registerShutdownHook()

package book.chapter4;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class BeanDemo {
	
	@Test
	public void testLoad$Close() {
		System.out.println("###### load  ##############");
		GenericXmlApplicationContext gtx = new GenericXmlApplicationContext();
		gtx.load("classpath:book/chapter4/app-context-a.xml") ;
		gtx.refresh();
		
		System.out.println("###### getBean ############");
		A bean = gtx.getBean("a" , A.class);
		bean.doSomething();
		
		System.out.println("###### close  #############");
		gtx.close();
	}
	@Test
	public void testRegisterShutdownHook() {
		System.out.println("###### load  ##############");
		GenericXmlApplicationContext gtx = new GenericXmlApplicationContext();
		gtx.registerShutdownHook();
		gtx.load("classpath:book/chapter4/app-context-a.xml") ;
		gtx.refresh();
		
		System.out.println("###### getBean ############");
		A bean = gtx.getBean("a" , A.class);
		bean.doSomething();
		
		System.out.println("###### close  #############");
	}
}

Run the test testRegisterShutdownHook. Resources can also be found release in the absence of close calls.
Here Insert Picture Description

ApplicationContextAware Interface

Finally, you can add a realization of the bean ApplicationContextAware interface

Will automatically scan the existence of GenericXmlApplicationContext,
there is, it automatically performs a registration methodregisterShutdownHook()

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	default-init-method="init" 
	default-destroy-method="destroyMethod"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:annotation-config />

<bean id="a" class="book.chapter4.A"  p:a="propA"/>
<bean class="book.chapter4.ShutdownHookBean" />

</beans>

Implements bean ApplicationContextAware interface

package book.chapter4;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericXmlApplicationContext;

public class ShutdownHookBean implements ApplicationContextAware {
	
	public ShutdownHookBean() {
		System.out.println("### ShutdownHookBean constructor ... ");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if(applicationContext instanceof GenericXmlApplicationContext) {
			GenericXmlApplicationContext gtx = (GenericXmlApplicationContext)applicationContext ;
			gtx.registerShutdownHook(); 
			System.out.println("#### registerShutdownHook() ... ");
		}
		
	}

}

Test class

package book.chapter4;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class BeanDemoHook {
	

	@Test
	public void testHookBean() {
		System.out.println("###### load  ##############");
		GenericXmlApplicationContext gtx = new GenericXmlApplicationContext();
		gtx.load("classpath:book/chapter4/app-context-xml-hook.xml") ;
		gtx.refresh();
		
		System.out.println("###### getBean ############");
		A bean = gtx.getBean("a" , A.class);
		bean.doSomething();
		
		System.out.println("###### close  #############");
	}
	

	
}

result

Here Insert Picture Description

Published 296 original articles · won praise 61 · views 7070

Guess you like

Origin blog.csdn.net/LawssssCat/article/details/103778145