Beans-cycle.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"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd ">
<bean id= "Car" class= "Com.hy.spring.beans.cycle.Car"
Init-method= "Init"
Destroy-method= "Destroy" >
<property name= "brand" value= "AuDi" ></property>
</bean>
</beans>
Car.java
Package com.hy.spring.beans.cycle;
public class Car {
Public Car () {
System.out.println ("Car ' s Constructor");
}
Private String brand;
public void Setbrand (String brand) {
System.out.println ("Setbrand ...");
This.brand = brand;
}
public void init () {
System.out.println ("Init .....");
}
public void Destroy () {
System.out.println ("Destroy ...");
}
}
Main.java
Package com.hy.spring.beans.cycle;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void Main (string[] args) {
Classpathxmlapplicationcontext CTX = new Classpathxmlapplicationcontext ("Beans-cycle.xml");
Car car = (car) ctx.getbean ("Car");
SYSTEM.OUT.PRINTLN (car);
Close IOC Container
Ctx.close ();
}
}
Results
XML configuration:
<!--
Implement the Beanpostprocessor interface and specifically provide
Object Postprocessbeforeinitialization (Object bean, String beanname): Init-method was called before
Object Postprocessafterinitialization (Object bean, String beanname): called after Init-method
The implementation.
Bean:bean the instance itself.
BEANNAME:IOC the name of the bean to which the container is configured.
Return value: Is the bean actually returned to the user, note: You can modify the returned bean in the above two methods, or even return a new bean
-
<!--configuration Bean's post processor: No configuration required ID,IOC container Auto-recognition is a beanpostprocessor-->
<bean id= "Mybeanpostprocessor" class= "Com.hy.spring.beans.cycle.MyBeanPostProcessor" >
</bean>
Mybeanpostprocessor.java
Package com.hy.spring.beans.cycle;
Import org.springframework.beans.BeansException;
Import Org.springframework.beans.factory.config.BeanPostProcessor;
public class Mybeanpostprocessor implements beanpostprocessor{
public object Postprocessafterinitialization (object bean, String beanname)
Throws Beansexception {
System.out.println ("postprocessafterinitialization:" + Bean + "," + beanname);
Car car = new car ();
Car.setbrand ("Ford");
return car;
}
public object Postprocessbeforeinitialization (object bean, String beanname)
Throws Beansexception {
if ("Car". Equals (Beanname)) {
//....
}
System.out.println ("postprocessbeforeinitialization:" + Bean + "," + beanname);
return bean;
}
}
Car.java
Package com.hy.spring.beans.cycle;
public class Car {
Public Car () {
System.out.println ("Car ' s Constructor");
}
Private String brand;
public void Setbrand (String brand) {
System.out.println ("Setbrand ...");
This.brand = brand;
}
public void init () {
System.out.println ("Init .....");
}
public void Destroy () {
System.out.println ("Destroy ...");
}
@Override
Public String toString () {
Return "Car [brand=" + Brand + "]";
}
}
Results:
Spring_ managing the life cycle of a Bean