Write code simple comparison Init-method,afterpropertiesset and beanpostprocessor

Source: Internet
Author: User
Tags comparison tomcat server

First, Brief introduction

1, the Init-method method, the initialization of the bean when the execution, can be configured for a specific bean. Init-method needs to be stated in the bean definition in the Applicationcontext.xml configuration document. For example: <bean id= "Testbean" class= "Nju.software.xkxt.util.TestBean" init-method= "init" ></bean> So, The Init method defined in Testbean is executed when the Testbean is initialized.
2, the Afterpropertiesset method, the initialization of the bean when the execution, can be configured for a specific bean. The Afterpropertiesset must implement the Initializingbean interface. Implementing the Initializingbean interface must implement the Afterpropertiesset method.
3, beanpostprocessor, for all beans in all spring contexts, You can configure a beanpostprocessor in the configuration document Applicationcontext.xml, and then perform a proxy for all beans before and after initialization. There are two methods in the Beanpostprocessor interface: Postprocessbeforeinitialization and postprocessafterinitialization. The Postprocessbeforeinitialization method executes before the bean is initialized, and the Postprocessafterinitialization method executes after the bean is initialized.
In summary, the order of execution between Afterpropertiesset and Init-method is performed Afterpropertiesset first, Init-method. From the role of beanpostprocessor, it can be seen that the first execution is postprocessbeforeinitialization, then Afterpropertiesset, then Init-method, And then the postprocessafterinitialization.
Second, related usage and Code Test 1, Postprocessor class, realize Beanpostprocessor interface, realize the postprocessbeforeinitialization in the interface, Postprocessafterinitialization method

Package nju.software.xkxt.util;

Import org.springframework.beans.BeansException;
Import Org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Defines the actions before and after Bean initialization
 * 
 * @author Typ * */Public
class Postprocessor implements beanpostprocessor {
	@Override public
	Object Postprocessbeforeinitialization (Object bean, String beanname)
			throws Beansexception {
		System.out.println ("------------------------------");
		System.out.println ("Object" + Beanname + "start instantiation");
		return bean;
	}

	@Override Public
	Object Postprocessafterinitialization (Object bean, String beanname)
			throws Beansexception {
		System.out.println ("Object" + Beanname + "instantiation complete");
		System.out.println ("------------------------------");
		return bean;
	}

}

The Postprocessor class is to be defined as a bean in Applicationcontext.xml, as follows <bean class= "Nju.software.xkxt.util.PostProcessor" > </bean>
2, Testbean class, used as Test bean, observe the sequence and content of the above 4 methods execution in the process of initialization of the bean. Implement the Initializingbean interface and implement the Afterpropertiesset method in the interface. Finally, define the Init method as Init-method.
Package nju.software.xkxt.util;

Import Org.springframework.beans.factory.InitializingBean;

/**
 * Used as a test bean to observe the sequence and contents of the above 4 methods executed during initialization of the bean
 * 
 * @author Typ * * */Public
class Testbean Implements Initializingbean {
	String name;

	Public String GetName () {
		return name;
	}

	public void SetName (String name) {
		this.name = name;
	}

	public void init () {
		System.out.println ("Init-method is called");
		System.out.println ("******************************");
	}

	@Override public
	void Afterpropertiesset () throws Exception {
		System.out.println ("*********************** *******");
		System.out.println ("Afterpropertiesset is called");
		System.out.println ("******************************");
	}
}

Starting the Tomcat server, you can see that the bean is initialized during the server startup process. The results of the implementation are as follows:
------------------------------Object Testbean begins instantiating ****************************** afterpropertiesset is  Called ****************************** init-method is called ****************************** Object Testbean instantiation Complete------------------------------
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.