码迷,mamicode.com
首页 > 其他好文 > 详细

bean生命周期

时间:2015-11-20 19:55:26      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

那在spring中bean的生命周期究竟是怎样的呢

1. 容器寻找Bean的定义信息并将其实例化

2. 使用依赖注入,spring按照Bean定义信息配置Bean的所有属性

3. 如果Bean实现了BeanNameAware接口,工厂调用Bean的SetBeanName()方法传递Bean的ID

4. 如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身

5. 如果BeanPostProcessor和Bean关联,那么其postProcessBeforeInitialization()方法将被调用

6. 如果Bean指定了init-method方法,将被调用

7. 最后,如果有BeanPostProcessor和Bean关联,那么其postProcessAfterInitialization()方法将被调用

此时,Bean已经可以被应用系统使用,并将被保留在BeanFactory中知道他不再被需要。有两种可以将其从BeanFactory中删除掉的方法

① 如果Bean实现了DisposableBean接口,destroy()方法将被调用

② 如指定了定制的销毁方法,就调用这个方法

 

public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter{

//before construct
@Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("before beanclass : " + beanClass.getName() + beanName);
return null;
}

//after construct
@Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("after beanclass : " + bean + beanName);
return true;
}

//before set filled
@Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
String beanName) throws BeansException {
System.out.println("postProcessPropertyValues");
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
}


}

public class MyBeanPostProcessor implements BeanPostProcessor {

//before init
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization");
return bean;
}


//after init
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization");
return bean;
}
}

public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {

private String brand;

private String color;

private int maxSpeed;

private String beanName;

public String getBrand() {
return brand;
}

public Car() {
System.out.println("construct");
}

public void setBrand(String brand) {
System.out.println("set");
this.brand = brand;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public int getMaxSpeed() {
return maxSpeed;
}

public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}

@Override
public String toString() {
return "Car{" + "brand=‘" + brand + ‘\‘‘ + ", color=‘" + color + ‘\‘‘ + ", maxSpeed=" + maxSpeed + ‘}‘;
}

//after set setBeanName
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("beanFactoryAware : " + beanFactory.containsBean(beanName));
}

//after set filed
@Override
public void setBeanName(String name) {
System.out.println("setbeanname : " + name);
this.beanName = name;
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("after properties set");
}

@Override public void destroy() throws Exception {
System.out.println("destory");
}

//init
public void init() {
System.out.println("init");
}

public void destory() {
System.out.println("destory");
}
}

//加载类到beanFactory,bean生命周期
@Test
public void test3() {
String path = Demo1.class.getResource("/").getPath().concat("beans.xml");


FileSystemResource resource = new FileSystemResource(path);

XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
xmlBeanFactory.addBeanPostProcessor(new MyBeanPostProcessor());
xmlBeanFactory.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());

Car car = (Car)xmlBeanFactory.getBean("car1");
Car car2 = (Car)xmlBeanFactory.getBean("car1");
System.out.println(car.toString());
xmlBeanFactory.destroyBean("car1");

}

bean生命周期

标签:

原文地址:http://www.cnblogs.com/jsy306/p/4981632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!