The life cycle method of Bean in spring

Source: Internet
Author: User

Bean's life cycle approach

Src\dayday\car.java

Package dayday;

Import Com.sun.org.apache.xpath.internal.SourceTree;

Import Javax.sound.midi.Soundbank;

/**
* Created by I am master on 2016/11/28.
*/
public class Car {
private String name;
public void SetName (String name) {
This.name=name;
System.out.println ("SetName ...");
}
Public String GetName () {
return name;
}
Public String toString () {
Return "[" + "Carname=" +name+ "]";
}
Public Car () {
System.out.println ("Car ' s constructor ...");
}
public void init () {
System.out.println ("init ...");
}
public void Destroy () {
System.out.println ("Destroy ...");
}
}

Src\dayday\main.java

Package dayday;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import java.net.SocketTimeoutException;
Import java.sql.SQLException;

/**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void Main (string[] args) throws SQLException, ClassNotFoundException {
Classpathxmlapplicationcontext ctx=new classpathxmlapplicationcontext ("Beans.xml");
Car Car=ctx.getbean ("car", car.class);
SYSTEM.OUT.PRINTLN (car);
Use the Close () method to invoke the bean's corresponding Destroy () method
Ctx.close ();
}
}

Src\beans.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" 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.xsd ">
<bean id= "Car" class= "dayday. Car "init-method=" Init "destroy-method=" destroy ">
<property name= "name" value= "Audi"/>
</bean>
</beans>

Run results

Car ' s constructor ...
SetName ...
Init...

[Carname=audi]
Destroy ...

The life cycle of a bean is managed in five steps

1) Create a bean instance by constructing a method or Factory method

2) Set values and references to other beans for the Bean's properties

3) Invocation of the Bean initialization method

4) using Beans

5) When the spring container is closed, the method of destroying the bean is called

Bean Post Processor

Additional operation of the bean before and after invoking initialization work

Need to implement Beanpostprocessor interface

Postprocessbeforeinitialization (Object Bean, String beanname) is called before the init () method is called

postprocessafterinitialization (Object Bean, String beanname) called after the init () method is called

Src\dayday\mybeanpostprocessor.java

Package   dayday;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
* Created by I am master on 2016/11/29.
*/
public class Mybeanpostprocessor implements beanpostprocessor {
@Override
Public Object Postprocessbeforeinitialization (Object bean, String beanname) throws beansexception {
System. Out. println ("postprocessbeforeinitialization"+bean+ ""+beanname);
return Bean;
}

@Override
Public Object Postprocessafterinitialization (Object bean, String beanname) throws beansexception {
System. Out. println ("postprocessafterinitialization"+bean+ ""+beanname);
return Bean;
}
}

Main/car.java code does not change

Src\bean.xml

<? XML version  encoding= "UTF-8"?>
<Beansxmlns= "Http://www.springframework.org/schema/beans"
xmlns:XSI= "Http://www.w3.org/2001/XMLSchema-instance"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.xsd " >
<BeanID= "Car"class= "Dayday." Car "Init-method= "Init"Destroy-method= "Destroy">
< Propertyname= "Name"value= "Audi"/>
</Bean>
<!--ConfigurationBeanThe Post processor-
<Beanclass= "Dayday." Mybeanpostprocessor "></Bean>
</Beans>
Operation Result:
Car ' s constructor...setname...postprocessbeforeinitialization [Carname=audi] carinit ... Postprocessafterinitialization [Carname=audi] car
[Carname=audi]destroy ...


Calling close () means that the Destroy () method in the bean is called

The life cycle method of Bean in spring

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.