Spring 4:生命周期

=================================================

SpringBean类的生命周期

在ApplicationContext中Bean的生命周期

生命周期执行的过程如下:
1)需找所有的bean根据bean定义的信息来实例化bean
默认bean都是单例
2)使用依赖注入,spring按bean定义信息配置bean的所有属性
3若bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递bean的ID。
4)若bean实现了BeanFactoryAware接口,工厂调用setBeanFactory() 方法传入工厂自身。
5)若bean实现了ApplicationContextAware()接口,setApplicationContext()方法会被调用
6)若bean实现了InitializingBean,则
afterPropertiesSet被调用
7)若bean指定了init-method="init"方法,它将被调用。
8)若BeanPostProcessor和bean关联,
则它们的postProcessBeforeInitialization()方法被调用
9)、若有BeanPostProcessor和bean关联,
     则它们的postProcessAfterInitialization()方法被调用
    注意:通过已上操作,此时的Bean就可以被应用的系统使用,并将保留在BeanFactory工厂中直到不再需要为止.但我们也可以通过10或者11进行销毁
10)、若bean实现了DisposableBean接口,distroy()方法被调用
11)、如果指定了destroy-method="close"定制的销毁方法,就调用这个方法

例:

package com.briup.bean;

public class Student {
	private long id;
	private String name;
	private int age;
	private String email;
	
	public void stu_init(){
		System.out.println("stu_init....");
	}
	public void destory(){
		System.out.println("stu...destory");
	}
	
	public Student() {
	}
	public Student(long id, String name, int age, String email) {
		System.out.println("&&&");
		this.id = id;
		this.name = name;
		this.age = age;
		this.email = email;
	}
		@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", email=" + email + "]";
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
}



<?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"
          >
          <!-- 对象的生命周期
          创建scope 初始化  .....销毁
          
          init-method对象构建初始化的方法
          destroy-method对象销毁执行的方法
          对象的销毁要容器销毁的时候才会触发 -->
          <bean name="stu" class="com.briup.bean.Student" 
          init-method="stu_init" 
          	destroy-method="destory">
          	<property name="id" value="1"></property>
          	<property name="name" value="jake"></property>
          	<property name="age" value="33"></property>
          	<property name="email" value="[email protected]"></property>
          </bean>
</beans>



@Test
	public void life_test(){
		try {
			ClassPathXmlApplicationContext cp=
					new ClassPathXmlApplicationContext(
							"com/briup/IOC/life.xml");
			System.out.println(cp.getBean("stu"));
			//cp.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_42857603/article/details/83278829
今日推荐