First: Actions before initializing and destroying beans by annotating @postconstruct and @PreDestroy methods
[Java]View Plain Copy
- Import javax.annotation.PostConstruct;
- Import Javax.annotation.PreDestroy;
- Public class datainitializer{
- @PostConstruct
- public void Initmethod () throws Exception {
- System.out.println ("Initmethod is executed");
- }
- @PreDestroy
- public void Destroymethod () throws Exception {
- System.out.println ("Destroymethod is executed");
- }
- }
The second is: by defining the Init-method and Destory-method methods in XML
[Java]View Plain Copy
- Public class datainitializer{
- public void Initmethod () throws Exception {
- System.out.println ("Initmethod is executed");
- }
- public void Destroymethod () throws Exception {
- System.out.println ("Destroymethod is executed");
- }
- }
[HTML]View Plain Copy
- <Bean id="Datainitializer" class="Com.somnus.demo.DataInitializer" init-method=" Initmethod " destory-method="destroymethod "/>
The third is the implementation of the Initializingbean and Disposablebean interfaces via beans
[Java]View Plain Copy
- Import Org.springframework.beans.factory.DisposableBean;
- Public class Datainitializer implements initializingbean,disposablebean{
- @Override
- public void Afterpropertiesset () throws Exception {
- System.out.println ("Afterpropertiesset is executed");
- }
- @Override
- public Void Destroy () throws Exception {
- System.out.println ("destroy is executed");
- }
- }
The first and the second are the same form, but one XML configuration, the other is the annotation form, there is a big difference is the third kind,
If a method is executed when the same bean is initialized in two ways, it is first shown in the order of execution.
Execute Afterpropertiesset () First,
Post-execution Initmethod ()
Here we look at the source code
How does this approach be implemented in spring?
By looking at the source Class (Abstractautowirecapablebeanfactory) of spring's load bean, you can see the secret
Abstractautowirecapablebeanfactory class in the Invokeinitmethods explained very clearly, the source code is as follows:
[Java]View Plain Copy
- protected void Invokeinitmethods (String beanname, final Object Bean, rootbeandefinition mbd)
- throws Throwable {
- ///To determine if the bean implements the Initializingbean interface, and if the Initializingbean interface is implemented, only the Afterpropertiesset method of invoking the bean is dropped
- Boolean Isinitializingbean = (bean instanceof Initializingbean);
- if (Isinitializingbean && (mbd = = Null | |!mbd.isexternallymanagedinitmethod ("Afterpropertiesset" ))) {
- if (logger.isdebugenabled ()) {
- Logger.debug ("invoking Afterpropertiesset () on beans with name '" + beanname + "'");
- }
- if (system.getsecuritymanager () = null) {
- try {
- Accesscontroller.doprivileged (new privilegedexceptionaction<object> () {
- Public Object Run () throws Exception {
- //Call Afterpropertiesset directly
- ((Initializingbean) bean). Afterpropertiesset ();
- return null;
- }
- },getaccesscontrolcontext ());
- } catch (Privilegedactionexception PAE) {
- throw pae.getexception ();
- }
- }
- else {
- //Call Afterpropertiesset directly
- ((Initializingbean) bean). Afterpropertiesset ();
- }
- }
- if (mbd! = null) {
- String initmethodname = Mbd.getinitmethodname ();
- //Determines whether the Init-method method is specified, and if the Init-method method is specified, then the established Init-method is called
- if (initmethodname ! = NULL &&! ( Isinitializingbean && "Afterpropertiesset". Equals (initmethodname) &&
- !mbd.isexternallymanagedinitmethod (Initmethodname)) {
- ///To see the source of the method further, it can be found that the method specified in the Init-method method is implemented by reflection
- Invokecustominitmethod (Beanname, Bean, mbd);
- }
- }
Summarize:
1:spring provides two ways for the bean to initialize the bean, implement the Initializingbean interface, implement the Afterpropertiesset method, or in the configuration file
Init-method , two different ways can be used simultaneously
2: Implementing the Initializingbean interface is a direct call to the Afterpropertiesset method, which is more efficient than the method specified by the reflection call Init-method. But
Init-method Way to eliminate the reliance on spring
3: If an error occurs when calling the Afterpropertiesset method, the method specified by Init-method is not called.
The spring container initializes the bean and takes action before destroying it