spring bean 生命周期
spring bean的生命周期从容器启动开始到容器停止运行,如图
代码如下
package com.spring.model;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Person implements DisposableBean, InitializingBean {
private String name;
Person() {
System.out.println("Constructor of person bean is invoked!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//Bean initialization code
public void afterPropertiesSet() throws Exception {
System.out.println("Initializing method of person bean is invoked!");
}
//Bean destruction code
public void destroy() throws Exception {
System.out.println("Destroy method of person bean is invoked!");
}
public void customDestroy() throws Exception {
System.out.println("Custom destroy method of person bean is invoked!");
}
public void customInit() throws Exception {
System.out.println("Custom initializing method of person bean is invoked!");
}
}
package com.spring.processer;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor{
// simply return the instantiated bean as-is
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println("Bean '" + beanName + "' before created : " + bean.toString());
return bean; // we could potentially return any object reference here...
}
public Object postProcessAfterInitialization(Object bean, String beanName) {
System.out.println("Bean '" + beanName + "' created : " + bean.toString());
return bean;
}
}
package com.spring.main;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.model.Person;
public class Demoapp {
public static void main(String[] args) {
// Reading configuration from the spring configuration file.
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Person myperson = context.getBean("personBean", Person.class);
System.out.println("Name= " + myperson.getName());
// Closing the context object.
context.close();
}
}
spring 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 name="personBean" class="com.spring.model.Person"> <property
name="name" value="Jason Clarke" /> </bean> -->
<bean name="personBean" class="com.spring.model.Person"
init-method="customInit" destroy-method="customDestroy">
<property name="name" value="Jason Clarke" />
</bean>
<!-- when the above bean (messenger) is instantiated, this custom BeanPostProcessor
implementation will output the fact to the system console -->
<bean class="com.spring.processer.InstantiationTracingBeanPostProcessor" />
</beans>
运行结果
Constructor of person bean is invoked!
Bean 'personBean' before created : com.spring.model.Person@604ed9f0
Initializing method of person bean is invoked!
Custom initializing method of person bean is invoked!
Bean 'personBean' created : com.spring.model.Person@604ed9f0
Name= Jason Clarke
Destroy method of person bean is invoked!
Custom destroy method of person bean is invoked!