s Afterpropertiesset Method and Init-method configuration description in Pring
1. Initializingbean.afterpropertiesset ()
The Initializingbean interface class in spring provides a way for the bean to define the initialization method, which contains only one method: Afterpropertiesset ().
The bean implements this interface and writes the initialization code in Afterpropertiesset ():
It is not necessary to have a special configuration for the bean in the XML configuration file, and spring checks that the bean implements the Initializingbean interface after the configuration file has completed all of the bean's assignments. Call the Bean's Afterpropertiesset method if implemented.
2. Init-method Configuration
Although spring can call this bean customization method by Initializingbean completion of a bean initialization, this approach requires the bean to implement the Initializingbean interface. Once the bean implements the Initializingbean interface, the bean's code is coupled with spring. You can use the Init-method functionality provided by spring to perform a bean-custom initialization method.
In the following code, the class Monitorkafka does not implement any spring interfaces, and defines a method monitorkafkamsg () without parameters.
Note: Destroy-method is the specified method called before the Bean is destroyed.
The Monitorkafkamsg () method in the Init-method configuration will be called after the bean initialization is completed, and spring requires that the Init-method be a parameterless method, or an exception will be thrown, and spring will abort the bean's subsequent processing. and throws a Org.springframework.beans.factory.BeanCreationException exception.
Summarize:
1. Initializingbean and Init-method can be used together, and spring will process the Initializingbean before processing Init-method. Init-method is performed through reflection, and Afterpropertiesset is executed directly, so afterpropertiesset is more efficient than Init-method, However, Init-method eliminates the bean dependency on spring and recommends the use of Init-method.
2. If a bean is defined as non-singleton, then Afterpropertiesset and Init-method are executed when each instance of the bean is created. The Afterpropertiesset and Init-method of a singleton bean are only called once when the Bean is first instantiated by an instance. In general, Afterpropertiesset and Init-method are applied on a single bean.
3. @PostConstruct and @predestory can implement similar functions by annotating them on a class-method basis.
Package COM.WILEY.BEGINNINGSPRING.EXERCISES.CH2;
Import javax.annotation.PostConstruct;
Import Org.springframework.beans.factory.InitializingBean;
public class Foo implements Initializingbean {
@Override
Public void Afterpropertiesset () throws Exception {
System.out.println ("Afterpropertiesset method is called");
}
@PostConstruct
Public void Init () {
System.out.println ("Init method is called");
}
Public Void Initialize () {
System.out.println ("Initialize method is called");
}
}
Afterpropertiesset method and Init-method configuration description in spring